-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathuseLockScroll.ts
83 lines (65 loc) · 2.43 KB
/
useLockScroll.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Ref, watch, onMounted, onActivated, onBeforeUnmount, onDeactivated, nextTick } from 'vue';
import { useTouch } from '../_util/useTouch';
import getScrollParent from '../_util/getScrollParent';
const totalLockCount = new Map<String, number>();
let mounted: boolean = null;
// 移植自vant:https://github.com/youzan/vant/blob/HEAD/src/composables/use-lock-scroll.ts
export function useLockScroll(rootRef: Ref<HTMLElement | undefined>, shouldLock: () => boolean, componentName: string) {
const touch = useTouch();
const BODY_LOCK_CLASS = `${componentName}--lock`;
const onTouchMove = (event: TouchEvent) => {
touch.move(event);
const direction = touch.deltaY.value > 0 ? '10' : '01';
const el = getScrollParent(event.target as Element, rootRef.value) as HTMLElement;
if (!el) return;
const { scrollHeight, offsetHeight, scrollTop } = el;
let status = '11';
if (scrollTop === 0) {
status = offsetHeight >= scrollHeight ? '00' : '01';
} else if (scrollTop + offsetHeight >= scrollHeight) {
status = '10';
}
if (status !== '11' && touch.isVertical() && !(parseInt(status, 2) & parseInt(direction, 2))) {
if (event.cancelable) {
event.preventDefault();
}
}
};
const lock = () => {
document.addEventListener('touchstart', touch.start);
document.addEventListener('touchmove', onTouchMove, { passive: false });
if (!totalLockCount.get(BODY_LOCK_CLASS)) {
document.body.classList.add(BODY_LOCK_CLASS);
}
totalLockCount.set(BODY_LOCK_CLASS, (totalLockCount.get(BODY_LOCK_CLASS) ?? 0) + 1);
};
const unlock = () => {
const sum = Array.from(totalLockCount.values()).reduce((acc, val) => acc + val, 0);
if (sum) {
document.removeEventListener('touchstart', touch.start);
document.removeEventListener('touchmove', onTouchMove);
totalLockCount.set(BODY_LOCK_CLASS, Math.max((totalLockCount.get(BODY_LOCK_CLASS) ?? 0) - 1, 0));
if (!totalLockCount.get(BODY_LOCK_CLASS)) {
document.body.classList.remove(BODY_LOCK_CLASS);
}
}
};
const init = () => shouldLock() && lock();
const destroy = () => shouldLock() && unlock();
onMounted(() => {
init();
nextTick(() => {
mounted = true;
});
});
onActivated(() => {
if (mounted) {
init();
}
});
onDeactivated(destroy);
onBeforeUnmount(destroy);
watch(shouldLock, (value) => {
value ? lock() : unlock();
});
}