diff --git a/src/App.vue b/src/App.vue index d8c953d..0ed5b06 100644 --- a/src/App.vue +++ b/src/App.vue @@ -77,14 +77,37 @@ import WechatModal from './components/WechatModal.vue' const showWechatModal = ref(false) -// 随机生成元素的初始位置 +// 优化动画性能 onMounted(() => { - const elements = document.querySelectorAll('.floating-element, .floating-bubble') + const elements = document.querySelectorAll('.floating-element, .floating-bubble, .floating-flower, .floating-cloud') elements.forEach(el => { el.style.left = `${Math.random() * 100}vw` el.style.top = `${Math.random() * 100}vh` + // 添加硬件加速 + el.style.transform = 'translateZ(0)' + el.style.willChange = 'transform' }) }) + +// 添加页面可见性检测 +onMounted(() => { + document.addEventListener('visibilitychange', handleVisibilityChange) +}) + +onUnmounted(() => { + document.removeEventListener('visibilitychange', handleVisibilityChange) +}) + +const handleVisibilityChange = () => { + const elements = document.querySelectorAll('.floating-element, .floating-bubble, .floating-flower, .floating-cloud') + elements.forEach(el => { + if (document.hidden) { + el.style.animationPlayState = 'paused' + } else { + el.style.animationPlayState = 'running' + } + }) +} diff --git a/src/components/ParticleBackground.vue b/src/components/ParticleBackground.vue index 968bf04..5052ec5 100644 --- a/src/components/ParticleBackground.vue +++ b/src/components/ParticleBackground.vue @@ -199,15 +199,14 @@ const animate = () => { const time = Date.now() * 0.00005 particles.material.uniforms.time.value = time - if (time % 2 === 0) { - updateParticles() - } + // 更新粒子位置 + updateParticles(time) renderer.render(scene, camera) } } -const updateParticles = () => { +const updateParticles = (time) => { const positions = particles.geometry.attributes.position.array for (let i = 0; i < config.particleCount; i++) {