@@ -12,8 +12,10 @@ let isLocked = false;
12
12
let initialClientY = - 1 ;
13
13
let initialClientX = - 1 ;
14
14
let 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
16
16
export 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' ;
17
19
18
20
const isIosDevice = ( ) => window . navigator
19
21
&& window . navigator . platform
@@ -74,19 +76,17 @@ function advancedUnlock(targetElement) {
74
76
document . removeEventListener ( 'touchmove' , preventDefault ) ;
75
77
}
76
78
77
- const isVerticalScroll = ( { scrollHeight, scrollWidth } ) => scrollHeight > scrollWidth ;
78
-
79
79
/**
80
80
* Handles the scrolling of the targetElement
81
81
* @param {TouchEvent } event
82
82
* @param {HTMLElement } targetElement
83
83
* @return {boolean }
84
84
*/
85
- function handleScroll ( event , target ) {
85
+ function handleScroll ( event , target , isHorizontal ) {
86
86
const clientY = event . targetTouches [ 0 ] . clientY - initialClientY ;
87
87
const clientX = event . targetTouches [ 0 ] . clientX - initialClientX ;
88
88
89
- if ( isVerticalScroll ( target ) ) {
89
+ if ( ! isHorizontal ) {
90
90
if ( target . scrollTop === 0 && clientY > 0 ) {
91
91
// element is at the top of its scroll.
92
92
return preventDefault ( event ) ;
@@ -110,7 +110,7 @@ function handleScroll(event, target) {
110
110
* Advanced scroll locking for iOS devices.
111
111
* @param targetElement
112
112
*/
113
- function advancedLock ( targetElement ) {
113
+ function advancedLock ( targetElement , isHorizontal = false ) {
114
114
// add a scroll listener to the body
115
115
document . addEventListener ( 'touchmove' , preventDefault , { passive : false } ) ;
116
116
if ( ! targetElement ) return ;
@@ -126,7 +126,7 @@ function advancedLock(targetElement) {
126
126
targetElement . ontouchmove = ( event ) => {
127
127
if ( event . targetTouches . length === 1 ) {
128
128
// detect single touch.
129
- handleScroll ( event , targetElement ) ;
129
+ handleScroll ( event , targetElement , isHorizontal ) ;
130
130
}
131
131
} ;
132
132
}
@@ -149,9 +149,12 @@ export default {
149
149
} else {
150
150
// lock everything but target element
151
151
advancedLock ( targetElement ) ;
152
- // lock everything but disabled targets
152
+ // lock everything but disabled targets with vertical scrolling
153
153
const disabledTargets = document . querySelectorAll ( `[${ SCROLL_LOCK_DISABLE_ATTR } ]` ) ;
154
154
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 ) ) ;
155
158
}
156
159
isLocked = true ;
157
160
} ,
@@ -167,7 +170,8 @@ export default {
167
170
advancedUnlock ( targetElement ) ;
168
171
// revert the old scroll position for disabled targets
169
172
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 ) ) ;
171
175
} else {
172
176
// remove all inline styles added by the `simpleLock` function
173
177
document . body . style . removeProperty ( 'overflow' ) ;
0 commit comments