@@ -12,8 +12,10 @@ let isLocked = false;
1212let initialClientY = - 1 ;
1313let initialClientX = - 1 ;
1414let scrolledClientY = 0 ;
15- // Adds this attribute to an inner scrollable element to allow it to scroll
15+ // Adds this attribute to an vertical scrollable element to allow it to scroll
1616export const SCROLL_LOCK_DISABLE_ATTR = 'data-scroll-lock-disable' ;
17+ // Adds this attribute to an horizontal scrollable element to allow it to scroll
18+ export const SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR = 'data-scroll-lock-horizontal-disable' ;
1719
1820const isIosDevice = ( ) => window . navigator
1921 && window . navigator . platform
@@ -74,19 +76,17 @@ function advancedUnlock(targetElement) {
7476 document . removeEventListener ( 'touchmove' , preventDefault ) ;
7577}
7678
77- const isVerticalScroll = ( { scrollHeight, scrollWidth } ) => scrollHeight > scrollWidth ;
78-
7979/**
8080 * Handles the scrolling of the targetElement
8181 * @param {TouchEvent } event
8282 * @param {HTMLElement } targetElement
8383 * @return {boolean }
8484 */
85- function handleScroll ( event , target ) {
85+ function handleScroll ( event , target , isHorizontal ) {
8686 const clientY = event . targetTouches [ 0 ] . clientY - initialClientY ;
8787 const clientX = event . targetTouches [ 0 ] . clientX - initialClientX ;
8888
89- if ( isVerticalScroll ( target ) ) {
89+ if ( ! isHorizontal ) {
9090 if ( target . scrollTop === 0 && clientY > 0 ) {
9191 // element is at the top of its scroll.
9292 return preventDefault ( event ) ;
@@ -110,7 +110,7 @@ function handleScroll(event, target) {
110110 * Advanced scroll locking for iOS devices.
111111 * @param targetElement
112112 */
113- function advancedLock ( targetElement ) {
113+ function advancedLock ( targetElement , isHorizontal = false ) {
114114 // add a scroll listener to the body
115115 document . addEventListener ( 'touchmove' , preventDefault , { passive : false } ) ;
116116 if ( ! targetElement ) return ;
@@ -126,7 +126,7 @@ function advancedLock(targetElement) {
126126 targetElement . ontouchmove = ( event ) => {
127127 if ( event . targetTouches . length === 1 ) {
128128 // detect single touch.
129- handleScroll ( event , targetElement ) ;
129+ handleScroll ( event , targetElement , isHorizontal ) ;
130130 }
131131 } ;
132132}
@@ -149,9 +149,12 @@ export default {
149149 } else {
150150 // lock everything but target element
151151 advancedLock ( targetElement ) ;
152- // lock everything but disabled targets
152+ // lock everything but disabled targets with vertical scrolling
153153 const disabledTargets = document . querySelectorAll ( `[${ SCROLL_LOCK_DISABLE_ATTR } ]` ) ;
154154 disabledTargets . forEach ( target => advancedLock ( target ) ) ;
155+ // lock everything but disabled targets with horizontal scrolling
156+ const disabledHorizontalTargets = document . querySelectorAll ( `[${ SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR } ]` ) ;
157+ disabledHorizontalTargets . forEach ( target => advancedLock ( target , true ) ) ;
155158 }
156159 isLocked = true ;
157160 } ,
@@ -167,7 +170,8 @@ export default {
167170 advancedUnlock ( targetElement ) ;
168171 // revert the old scroll position for disabled targets
169172 const disabledTargets = document . querySelectorAll ( `[${ SCROLL_LOCK_DISABLE_ATTR } ]` ) ;
170- disabledTargets . forEach ( target => advancedUnlock ( target ) ) ;
173+ const disabledHorizontalTargets = document . querySelectorAll ( `[${ SCROLL_LOCK_DISABLE_HORIZONTAL_ATTR } ]` ) ;
174+ [ ...disabledTargets , ...disabledHorizontalTargets ] . forEach ( target => advancedUnlock ( target ) ) ;
171175 } else {
172176 // remove all inline styles added by the `simpleLock` function
173177 document . body . style . removeProperty ( 'overflow' ) ;
0 commit comments