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) {