From 4bf7dcde791c3bab1915491bff4c28e160de26ca Mon Sep 17 00:00:00 2001 From: KQL Date: Thu, 4 Dec 2025 19:53:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E8=BF=9B=E5=85=A5=E6=97=B6UI=E4=B8=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=92=8C=E8=BD=AC=E5=9C=BA=E8=87=AA=E5=8A=A8=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 init3DScene() 中清除 uiLayer 和 hint 的内联 opacity 样式 - 在 SceneManager 中保存事件处理器引用 - 在 dispose() 中正确移除所有事件监听器 - 防止事件监听器重复绑定导致的意外触发 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- js/main.js | 5 +++-- js/scene/SceneManager.js | 28 +++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/js/main.js b/js/main.js index a210cea..68442fd 100644 --- a/js/main.js +++ b/js/main.js @@ -126,10 +126,11 @@ class App { this.mapInterface.style.display = 'none'; this.mapInterface.style.opacity = '0'; - // 重置UI层和提示文字的状态 + // 重置UI层和提示文字的状态(清除转场时设置的内联样式) this.uiLayer.style.display = 'block'; + this.uiLayer.style.opacity = ''; // 清除内联opacity,让CSS和GSAP控制 this.hint.style.display = 'block'; - // 不设置 opacity,让GSAP的 fromTo() 控制 + this.hint.style.opacity = ''; // 清除内联opacity // 创建场景管理器 this.sceneManager = new SceneManager( diff --git a/js/scene/SceneManager.js b/js/scene/SceneManager.js index 1f14d8e..86be696 100644 --- a/js/scene/SceneManager.js +++ b/js/scene/SceneManager.js @@ -154,20 +154,29 @@ export class SceneManager { // 绑定事件 bindEvents() { + // 保存事件处理器引用,以便后续移除 + this.eventHandlers = { + mouseMove: (e) => this.onMouseMove(e), + mouseDown: () => this.onMouseDown(), + touchMove: (e) => this.onTouchMove(e), + touchStart: (e) => this.onTouchStart(e), + resize: () => this.onWindowResize() + }; + // 鼠标移动 - window.addEventListener('mousemove', (e) => this.onMouseMove(e)); + window.addEventListener('mousemove', this.eventHandlers.mouseMove); // 鼠标点击 - window.addEventListener('mousedown', () => this.onMouseDown()); + window.addEventListener('mousedown', this.eventHandlers.mouseDown); // 触摸移动(移动端) - window.addEventListener('touchmove', (e) => this.onTouchMove(e), { passive: false }); + window.addEventListener('touchmove', this.eventHandlers.touchMove, { passive: false }); // 触摸点击(移动端) - window.addEventListener('touchstart', (e) => this.onTouchStart(e)); + window.addEventListener('touchstart', this.eventHandlers.touchStart); // 窗口resize - window.addEventListener('resize', () => this.onWindowResize()); + window.addEventListener('resize', this.eventHandlers.resize); } // 鼠标移动事件 @@ -290,6 +299,15 @@ export class SceneManager { // 销毁场景 dispose() { + // 移除事件监听器,防止内存泄漏和重复触发 + if (this.eventHandlers) { + window.removeEventListener('mousemove', this.eventHandlers.mouseMove); + window.removeEventListener('mousedown', this.eventHandlers.mouseDown); + window.removeEventListener('touchmove', this.eventHandlers.touchMove); + window.removeEventListener('touchstart', this.eventHandlers.touchStart); + window.removeEventListener('resize', this.eventHandlers.resize); + } + if (this.starSystem) this.starSystem.dispose(); if (this.earthModel) this.earthModel.dispose(); if (this.renderer) {