Skip to content

Support scrolling on both axes (iOS) #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions lib/bodyScrollLock.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ const isIosDevice = typeof window !== 'undefined' && window.navigator && window.

let locks = [];
let documentListenerAdded = false;
let initialClientY = -1;
let initialClient = { x: -1, y: -1 };
let previousBodyOverflowSetting;
let previousBodyPaddingRight;

let axis = null;

// returns true if `el` should be allowed to receive touchmove events.
const allowTouchMove = el => locks.some(lock => {
if (lock.options.allowTouchMove && lock.options.allowTouchMove(el)) {
Expand Down Expand Up @@ -100,21 +102,32 @@ const restoreOverflowSetting = () => {
};

// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
const isTargetElementTotallyScrolled = targetElement => targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
const isTargetElementTotallyScrolled = (targetElement, axis) => {
if (targetElement) {
const totalScroll = targetElement[`scroll${axis === 'y' ? 'Height' : 'Width'}`];
const scrolled = targetElement[`scroll${axis === 'y' ? 'Top' : 'Left'}`];
const clientSize = targetElement[`client${axis === 'y' ? 'Height' : 'Width'}`];
return totalScroll - scrolled <= clientSize;
}
return false;
};

const handleScroll = (event, targetElement) => {
const clientY = event.targetTouches[0].clientY - initialClientY;
const handleScroll = (event, targetElement, axis) => {
const touch = event.targetTouches[0];
const initialPos = initialClient[axis];
const scrollPos = targetElement && targetElement[`scroll${axis === 'y' ? 'Top' : 'Left'}`];
const clientPos = (axis === 'y' ? touch.clientY : touch.clientX) - initialPos;

if (allowTouchMove(event.target)) {
return false;
}

if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
if (targetElement && scrollPos === 0 && clientPos > 0) {
// element is at the top of its scroll.
return preventDefault(event);
}

if (isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
if (isTargetElementTotallyScrolled(targetElement, axis) && clientPos < 0) {
// element is at the top of its scroll.
return preventDefault(event);
}
Expand Down Expand Up @@ -144,13 +157,22 @@ export const disableBodyScroll = (targetElement, options) => {
targetElement.ontouchstart = event => {
if (event.targetTouches.length === 1) {
// detect single touch.
initialClientY = event.targetTouches[0].clientY;
initialClient = {
x: event.targetTouches[0].clientX,
y: event.targetTouches[0].clientY
};
axis = null;
}
};
targetElement.ontouchmove = event => {
if (event.targetTouches.length === 1) {
// detect single touch.
handleScroll(event, targetElement);
if (!axis) {
const distX = Math.abs(initialClient.x - event.targetTouches[0].clientX);
const distY = Math.abs(initialClient.y - event.targetTouches[0].clientY);
axis = distX > distY ? 'x' : 'y';
}
handleScroll(event, targetElement, axis);
}
};

Expand Down Expand Up @@ -186,7 +208,8 @@ export const clearAllBodyScrollLocks = () => {
locks = [];

// Reset initial clientY.
initialClientY = -1;
initialClient = { x: -1, y: -1 };
axis = null;
} else {
restoreOverflowSetting();
locks = [];
Expand Down
41 changes: 31 additions & 10 deletions lib/bodyScrollLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@

var locks = [];
var documentListenerAdded = false;
var initialClientY = -1;
var initialClient = { x: -1, y: -1 };
var previousBodyOverflowSetting = void 0;
var previousBodyPaddingRight = void 0;

var axis = null;

// returns true if `el` should be allowed to receive touchmove events.
var allowTouchMove = function allowTouchMove(el) {
return locks.some(function (lock) {
Expand Down Expand Up @@ -131,23 +133,32 @@
};

// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
var isTargetElementTotallyScrolled = function isTargetElementTotallyScrolled(targetElement) {
return targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
var isTargetElementTotallyScrolled = function isTargetElementTotallyScrolled(targetElement, axis) {
if (targetElement) {
var totalScroll = targetElement['scroll' + (axis === 'y' ? 'Height' : 'Width')];
var scrolled = targetElement['scroll' + (axis === 'y' ? 'Top' : 'Left')];
var clientSize = targetElement['client' + (axis === 'y' ? 'Height' : 'Width')];
return totalScroll - scrolled <= clientSize;
}
return false;
};

var handleScroll = function handleScroll(event, targetElement) {
var clientY = event.targetTouches[0].clientY - initialClientY;
var handleScroll = function handleScroll(event, targetElement, axis) {
var touch = event.targetTouches[0];
var initialPos = initialClient[axis];
var scrollPos = targetElement && targetElement['scroll' + (axis === 'y' ? 'Top' : 'Left')];
var clientPos = (axis === 'y' ? touch.clientY : touch.clientX) - initialPos;

if (allowTouchMove(event.target)) {
return false;
}

if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
if (targetElement && scrollPos === 0 && clientPos > 0) {
// element is at the top of its scroll.
return preventDefault(event);
}

if (isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
if (isTargetElementTotallyScrolled(targetElement, axis) && clientPos < 0) {
// element is at the top of its scroll.
return preventDefault(event);
}
Expand Down Expand Up @@ -179,13 +190,22 @@
targetElement.ontouchstart = function (event) {
if (event.targetTouches.length === 1) {
// detect single touch.
initialClientY = event.targetTouches[0].clientY;
initialClient = {
x: event.targetTouches[0].clientX,
y: event.targetTouches[0].clientY
};
axis = null;
}
};
targetElement.ontouchmove = function (event) {
if (event.targetTouches.length === 1) {
// detect single touch.
handleScroll(event, targetElement);
if (!axis) {
var distX = Math.abs(initialClient.x - event.targetTouches[0].clientX);
var distY = Math.abs(initialClient.y - event.targetTouches[0].clientY);
axis = distX > distY ? 'x' : 'y';
}
handleScroll(event, targetElement, axis);
}
};

Expand Down Expand Up @@ -221,7 +241,8 @@
locks = [];

// Reset initial clientY.
initialClientY = -1;
initialClient = { x: -1, y: -1 };
axis = null;
} else {
restoreOverflowSetting();
locks = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/bodyScrollLock.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 33 additions & 10 deletions src/bodyScrollLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ type HandleScrollEvent = TouchEvent;

let locks: Array<Lock> = [];
let documentListenerAdded: boolean = false;
let initialClientY: number = -1;
let initialClient: { x: number, y: number } = { x: -1, y: -1 };
let previousBodyOverflowSetting;
let previousBodyPaddingRight;

type Axis = 'x' | 'y';
let axis: ?Axis = null;

// returns true if `el` should be allowed to receive touchmove events.
const allowTouchMove = (el: EventTarget): boolean =>
locks.some(lock => {
Expand Down Expand Up @@ -113,22 +116,32 @@ const restoreOverflowSetting = () => {
};

// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
const isTargetElementTotallyScrolled = (targetElement: any): boolean =>
targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
const isTargetElementTotallyScrolled = (targetElement: any, axis: Axis): boolean => {
if (targetElement) {
const totalScroll = targetElement[`scroll${axis === 'y' ? 'Height' : 'Width'}`];
const scrolled = targetElement[`scroll${axis === 'y' ? 'Top' : 'Left'}`];
const clientSize = targetElement[`client${axis === 'y' ? 'Height' : 'Width'}`];
return totalScroll - scrolled <= clientSize;
}
return false;
};

const handleScroll = (event: HandleScrollEvent, targetElement: any): boolean => {
const clientY = event.targetTouches[0].clientY - initialClientY;
const handleScroll = (event: HandleScrollEvent, targetElement: any, axis: Axis): boolean => {
const touch = event.targetTouches[0];
const initialPos = initialClient[axis];
const scrollPos = targetElement && targetElement[`scroll${axis === 'y' ? 'Top' : 'Left'}`];
const clientPos = (axis === 'y' ? touch.clientY : touch.clientX) - initialPos;

if (allowTouchMove(event.target)) {
return false;
}

if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
if (targetElement && scrollPos === 0 && clientPos > 0) {
// element is at the top of its scroll.
return preventDefault(event);
}

if (isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
if (isTargetElementTotallyScrolled(targetElement, axis) && clientPos < 0) {
// element is at the top of its scroll.
return preventDefault(event);
}
Expand Down Expand Up @@ -160,13 +173,22 @@ export const disableBodyScroll = (targetElement: any, options?: BodyScrollOption
targetElement.ontouchstart = (event: HandleScrollEvent) => {
if (event.targetTouches.length === 1) {
// detect single touch.
initialClientY = event.targetTouches[0].clientY;
initialClient = {
x: event.targetTouches[0].clientX,
y: event.targetTouches[0].clientY,
};
axis = null;
}
};
targetElement.ontouchmove = (event: HandleScrollEvent) => {
if (event.targetTouches.length === 1) {
// detect single touch.
handleScroll(event, targetElement);
if (!axis) {
const distX = Math.abs(initialClient.x - event.targetTouches[0].clientX);
const distY = Math.abs(initialClient.y - event.targetTouches[0].clientY);
axis = distX > distY ? 'x' : 'y';
}
handleScroll(event, targetElement, axis);
}
};

Expand Down Expand Up @@ -202,7 +224,8 @@ export const clearAllBodyScrollLocks = (): void => {
locks = [];

// Reset initial clientY.
initialClientY = -1;
initialClient = { x: -1, y: -1 };
axis = null;
} else {
restoreOverflowSetting();
locks = [];
Expand Down