From 5ed89b0b0258930f3ccdc3d6f9ec36a8bb8efe74 Mon Sep 17 00:00:00 2001 From: hkaikai <617760820@qq.com> Date: Tue, 29 Oct 2024 20:18:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(uselockscroll.ts):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=A4=9A=E7=BB=84=E4=BB=B6=E5=90=8C=E6=97=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?preventScrollThrough=E6=97=B6,=E5=AF=BC=E8=87=B4=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E9=80=80=E5=87=BA=E7=A6=81=E6=AD=A2=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #1614 --- src/hooks/useLockScroll.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/hooks/useLockScroll.ts b/src/hooks/useLockScroll.ts index f38734798..dd07a8903 100644 --- a/src/hooks/useLockScroll.ts +++ b/src/hooks/useLockScroll.ts @@ -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(); let mounted: boolean = null; // 移植自vant:https://github.com/youzan/vant/blob/HEAD/src/composables/use-lock-scroll.ts @@ -37,21 +37,22 @@ export function useLockScroll(rootRef: Ref, 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 = 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) - 1, 0)); - if (!totalLockCount) { + if (!totalLockCount.get(BODY_LOCK_CLASS)) { document.body.classList.remove(BODY_LOCK_CLASS); } } From 52fe0632ecb29e68ae84e9c991324db82c461bca Mon Sep 17 00:00:00 2001 From: hkaikai <617760820@qq.com> Date: Tue, 29 Oct 2024 20:31:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?style:=20totalLockCount=20=E5=8F=96?= =?UTF-8?q?=E5=80=BC=E7=A9=BA=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useLockScroll.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useLockScroll.ts b/src/hooks/useLockScroll.ts index dd07a8903..85a6dea86 100644 --- a/src/hooks/useLockScroll.ts +++ b/src/hooks/useLockScroll.ts @@ -45,12 +45,12 @@ export function useLockScroll(rootRef: Ref, shouldLock: }; const unlock = () => { - const sum = totalLockCount.values().reduce((acc, val) => acc + val, 0); + 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) - 1, 0)); + 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);