Skip to content

Commit

Permalink
fix(uselockscroll.ts): 修复多组件同时使用preventScrollThrough时,导致无法退出禁止滑动模式的问题 (
Browse files Browse the repository at this point in the history
…#1628)

* fix(uselockscroll.ts): 修复多组件同时使用preventScrollThrough时,导致无法退出禁止滑动模式的问题

fix #1614

* style: totalLockCount 取值空判断
  • Loading branch information
hkaikai authored Nov 19, 2024
1 parent 3ece456 commit cdb0e70
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/hooks/useLockScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTouch } from '../_util/useTouch';
import getScrollParent from '../_util/getScrollParent';
import { supportsPassive } from '../_util/supportsPassive';

let totalLockCount = 0;
const totalLockCount = new Map<String, number>();
let mounted: boolean = null;

// 移植自vant:https://github.com/youzan/vant/blob/HEAD/src/composables/use-lock-scroll.ts
Expand Down Expand Up @@ -37,21 +37,22 @@ export function useLockScroll(rootRef: Ref<HTMLElement | undefined>, shouldLock:
document.addEventListener('touchstart', touch.start);
document.addEventListener('touchmove', onTouchMove, supportsPassive.value ? { passive: false } : false);

if (!totalLockCount) {
if (!totalLockCount.get(BODY_LOCK_CLASS)) {
document.body.classList.add(BODY_LOCK_CLASS);
}

totalLockCount += 1;
totalLockCount.set(BODY_LOCK_CLASS, (totalLockCount.get(BODY_LOCK_CLASS) ?? 0) + 1);
};

const unlock = () => {
if (totalLockCount) {
const sum = Array.from(totalLockCount.values()).reduce((acc, val) => acc + val, 0);
if (sum) {
document.removeEventListener('touchstart', touch.start);
document.removeEventListener('touchmove', onTouchMove);

totalLockCount -= 1;
totalLockCount.set(BODY_LOCK_CLASS, Math.max((totalLockCount.get(BODY_LOCK_CLASS) ?? 0) - 1, 0));

if (!totalLockCount) {
if (!totalLockCount.get(BODY_LOCK_CLASS)) {
document.body.classList.remove(BODY_LOCK_CLASS);
}
}
Expand Down

0 comments on commit cdb0e70

Please sign in to comment.