From b374ca198bdc7a1482d356e31a1013403212e5d7 Mon Sep 17 00:00:00 2001 From: chenchun Date: Sun, 26 Apr 2026 20:50:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(lenis):=20=E6=94=B9=E8=BF=9B=E5=B9=B3?= =?UTF-8?q?=E6=BB=91=E6=BB=9A=E5=8A=A8=E6=8F=92=E4=BB=B6=E7=9A=84=E6=B8=85?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加销毁状态检查和浏览器卸载事件处理,确保在热更新和页面卸载时正确清理资源 --- app/plugins/lenis.client.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/plugins/lenis.client.ts b/app/plugins/lenis.client.ts index 886064e..e12f5c4 100644 --- a/app/plugins/lenis.client.ts +++ b/app/plugins/lenis.client.ts @@ -5,7 +5,7 @@ import { ScrollTrigger } from 'gsap/ScrollTrigger' gsap.registerPlugin(ScrollTrigger) -export default defineNuxtPlugin((nuxtApp) => { +export default defineNuxtPlugin(() => { // 用 Lenis 平滑浏览器原始 wheel 输入,避免鼠标滚轮一格跳太远。 const lenis = new Lenis({ lerp: 0.075, @@ -21,12 +21,22 @@ export default defineNuxtPlugin((nuxtApp) => { gsap.ticker.add(updateLenis) gsap.ticker.lagSmoothing(0) - // Nuxt 卸载时清掉 ticker 和事件,避免热更新或页面销毁后重复驱动滚动。 - nuxtApp.hook('app:beforeUnmount', () => { + let isDestroyed = false + const cleanupLenis = () => { + if (isDestroyed) { + return + } + + isDestroyed = true lenis.off('scroll', ScrollTrigger.update) gsap.ticker.remove(updateLenis) + window.removeEventListener('beforeunload', cleanupLenis) lenis.destroy() - }) + } + + // Nuxt 没有类型化的 app:beforeUnmount;这里用浏览器卸载和 Vite 热更新做清理。 + window.addEventListener('beforeunload', cleanupLenis) + import.meta.hot?.dispose(cleanupLenis) return { provide: {