10
10
11
11
let isLocked = false ;
12
12
let initialClientY = - 1 ;
13
+ let initialClientX = - 1 ;
13
14
let scrolledClientY = 0 ;
14
15
// Adds this attribute to an inner scrollable element to allow it to scroll
15
16
export const SCROLL_LOCK_DISABLE_ATTR = 'data-scroll-lock-disable' ;
@@ -82,8 +83,10 @@ const isVerticalScroll = ({ scrollHeight, scrollWidth }) => scrollHeight > scrol
82
83
* @return {boolean }
83
84
*/
84
85
function handleScroll ( event , target ) {
86
+ const clientY = event . targetTouches [ 0 ] . clientY - initialClientY ;
87
+ const clientX = event . targetTouches [ 0 ] . clientX - initialClientX ;
88
+
85
89
if ( isVerticalScroll ( target ) ) {
86
- const clientY = event . targetTouches [ 0 ] . clientY - initialClientY ;
87
90
if ( target . scrollTop === 0 && clientY > 0 ) {
88
91
// element is at the top of its scroll.
89
92
return preventDefault ( event ) ;
@@ -93,6 +96,9 @@ function handleScroll(event, target) {
93
96
// element is at the bottom of its scroll.
94
97
return preventDefault ( event ) ;
95
98
}
99
+ } else if ( Math . abs ( clientY ) > Math . abs ( clientX ) ) {
100
+ // prevent event if user tries to perform vertical scroll in an horizontal scrolling element
101
+ return preventDefault ( event ) ;
96
102
}
97
103
98
104
// prevent the scroll event from going up to the parent/window
@@ -114,6 +120,7 @@ function advancedLock(targetElement) {
114
120
if ( event . targetTouches . length === 1 ) {
115
121
// detect single touch.
116
122
initialClientY = event . targetTouches [ 0 ] . clientY ;
123
+ initialClientX = event . targetTouches [ 0 ] . clientX ;
117
124
}
118
125
} ;
119
126
targetElement . ontouchmove = ( event ) => {
0 commit comments