Skip to content

Commit 111ce11

Browse files
evinmaBuptStEve
authored andcommitted
Feature/scroll x ios (#31)
* feat: support scroll-x in demo * feat: support scroll-x on iOS with lock
1 parent e996a64 commit 111ce11

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

index.html

+8-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ <h2>
3737
dialog one
3838
<button id="modalBtn">click me to show dialog two</button>
3939
</h2>
40-
<p id="targetOne" class="target"></p>
40+
<div id="targetOne" class="target"></div>
4141
</div>
4242

4343
<div id="modalTwo" class="modal">
44-
<h2>dialog two</h2>
45-
<p id="targetTwo" class="target"></p>
44+
<h2>dialog two with scroll-x</h2>
45+
<div id="targetTwo" class="target">
46+
<p>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950</p>
47+
<p>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950</p>
48+
</div>
4649
</div>
4750

4851
<div id="list"></div>
@@ -63,7 +66,6 @@ <h2>dialog two</h2>
6366
}
6467
$('#list').innerHTML = someText
6568
$targetOne.innerHTML = someText
66-
$targetTwo.innerHTML = someText
6769

6870
$('#btn').onclick = function () {
6971
$modalOne.style.display = 'block'
@@ -104,8 +106,9 @@ <h2>dialog two</h2>
104106
header {
105107
position: fixed;
106108
top: 0;
107-
left: 0;
108109
right: 0;
110+
left: 0;
111+
109112
background: #fff;
110113
}
111114
.content {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tua-body-scroll-lock",
3-
"version": "0.3.1",
3+
"version": "1.0.0",
44
"description": "🔐Body scroll locking that just works with everything",
55
"main": "dist/tua-bsl.umd.js",
66
"module": "dist/tua-bsl.esm.js",

src/index.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type OverflowHiddenMobileStyleType = 'top' | 'width' | 'height' | 'overflow' | '
1010

1111
let lockedNum = 0
1212
let initialClientY = 0
13+
let initialClientX = 0
1314
let unLockCallback: any = null
1415
let documentListenerAdded = false
1516

@@ -67,14 +68,28 @@ const preventDefault = (event: TouchEvent) => {
6768
}
6869

6970
const handleScroll = (event: TouchEvent, targetElement: HTMLElement) => {
70-
const clientY = event.targetTouches[0].clientY - initialClientY
71-
7271
if (targetElement) {
73-
const { scrollTop, scrollHeight, clientHeight } = targetElement
72+
const {
73+
scrollTop,
74+
scrollLeft,
75+
scrollWidth,
76+
scrollHeight,
77+
clientWidth,
78+
clientHeight,
79+
} = targetElement
80+
const clientX = event.targetTouches[0].clientX - initialClientX
81+
const clientY = event.targetTouches[0].clientY - initialClientY
82+
const isVertical = Math.abs(clientY) > Math.abs(clientX)
83+
7484
const isOnTop = clientY > 0 && scrollTop === 0
85+
const isOnLeft = clientX > 0 && scrollLeft === 0
86+
const isOnRight = clientX < 0 && scrollLeft + clientWidth + 1 >= scrollWidth
7587
const isOnBottom = clientY < 0 && scrollTop + clientHeight + 1 >= scrollHeight
7688

77-
if (isOnTop || isOnBottom) {
89+
if (
90+
(isVertical && (isOnTop || isOnBottom)) ||
91+
(!isVertical && (isOnLeft || isOnRight))
92+
) {
7893
return preventDefault(event)
7994
}
8095
}
@@ -104,6 +119,7 @@ const lock = (targetElement?: HTMLElement) => {
104119
if (targetElement && lockedElements.indexOf(targetElement) === -1) {
105120
targetElement.ontouchstart = (event) => {
106121
initialClientY = event.targetTouches[0].clientY
122+
initialClientX = event.targetTouches[0].clientX
107123
}
108124

109125
targetElement.ontouchmove = (event) => {

0 commit comments

Comments
 (0)