diff --git a/src/App.vue b/src/App.vue index 0ed5b06..87cb395 100644 --- a/src/App.vue +++ b/src/App.vue @@ -77,37 +77,37 @@ import WechatModal from './components/WechatModal.vue' const showWechatModal = ref(false) -// 优化动画性能 +// 优化动画控制 +const handleVisibilityChange = () => { + const elements = document.querySelectorAll('.floating-element, .floating-bubble, .floating-flower, .floating-cloud') + const isHidden = document.hidden + + elements.forEach(el => { + if (isHidden) { + el.classList.add('animation-paused') + } else { + el.classList.remove('animation-paused') + } + }) +} + +// 优化初始化 onMounted(() => { 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' + el.style.animationPlayState = 'running' }) -}) - -// 添加页面可见性检测 -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' - } - }) -}