fix: 修复首页 Work 板块堆叠卡片滚动动画失效

- 根因:数据改为异步 useFetch 后,卡片在 ScrollTrigger 初始化之后才渲染,
  initScrollAnimation 因 count<2 提前 return,pin 永不生效,板块直接滚过
- 修复:数据就绪 + 卡片渲染后再初始化(watch works + onMounted 双 nextTick +
  hero-intro-done + 3s 保底),inited 幂等防重复
- 图片改 eager 加载,避免堆叠卡片滑动时空白
This commit is contained in:
2026-08-06 19:57:46 +08:00
parent b9ffcdb874
commit 058493f498
+31 -32
View File
@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, onUnmounted, ref, nextTick, computed } from 'vue' import { onMounted, onUnmounted, ref, nextTick, computed, watch } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import gsap from 'gsap' import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger' import { ScrollTrigger } from 'gsap/ScrollTrigger'
@@ -64,25 +64,21 @@ const partners = [
] ]
let ctx: gsap.Context | null = null let ctx: gsap.Context | null = null
let inited = false
const refresh = () => { const refresh = () => {
ScrollTrigger.refresh() ScrollTrigger.refresh()
} }
onMounted(async () => { const initScrollAnimation = () => {
if (inited) return
if (!rootRef.value || !workSectionRef.value || !workPinRef.value) return if (!rootRef.value || !workSectionRef.value || !workPinRef.value) return
await nextTick()
// 延迟初始化 ScrollTrigger,避免阻塞首屏 Hero 动画
// 等待 Hero 入场动画完成(约 3-4 秒)
const initScrollAnimation = () => {
ctx = gsap.context(() => {
const cards = cardRefs.value const cards = cardRefs.value
const count = cards.length const count = cards.length
if (count < 2) return if (count < 2) return
inited = true
ctx = gsap.context(() => {
const enterOffset = 160 const enterOffset = 160
const scaleTo = 0.92 const scaleTo = 0.92
const depthOffsetY = -18 const depthOffsetY = -18
@@ -111,16 +107,8 @@ onMounted(async () => {
const next = cards[i] const next = cards[i]
const start = i - 1 const start = i - 1
tl.to( tl.to(next, { y: 0, duration: 1, ease: 'power2.out' }, start)
next, tl.to(prev, { scale: scaleTo, y: depthOffsetY, duration: 0.6, ease: 'power2.out' }, start + 0.35)
{ y: 0, duration: 1, ease: 'power2.out' },
start,
)
tl.to(
prev,
{ scale: scaleTo, y: depthOffsetY, duration: 0.6, ease: 'power2.out' },
start + 0.35,
)
tl.set(prev, { scale: scaleTo, y: depthOffsetY }, start + 1) tl.set(prev, { scale: scaleTo, y: depthOffsetY }, start + 1)
} }
@@ -134,21 +122,31 @@ onMounted(async () => {
invalidateOnRefresh: true, invalidateOnRefresh: true,
animation: tl, animation: tl,
}) })
ScrollTrigger.refresh()
}, rootRef.value) }, rootRef.value)
}
// 监听 hero 动画完成事件 // 等待布局稳定后刷新(图片加载完成也会触发 @load -> refresh)
if (typeof window !== 'undefined') { requestAnimationFrame(() => ScrollTrigger.refresh())
window.addEventListener('hero-intro-done', initScrollAnimation, { once: true }) }
// 保底:3秒后必定初始化 // 幂等初始化:等数据就绪 + 卡片渲染后再执行(异步数据场景的关键修复)
setTimeout(() => { const tryInit = async () => {
window.removeEventListener('hero-intro-done', initScrollAnimation) await nextTick()
if (!ctx) { await nextTick()
initScrollAnimation() initScrollAnimation()
} }
watch(works, (val) => {
if (val && val.length) tryInit()
}, { immediate: true })
onMounted(() => {
// 数据在 await useFetch 后已就绪,直接尝试初始化
tryInit()
if (typeof window !== 'undefined') {
window.addEventListener('hero-intro-done', tryInit, { once: true })
setTimeout(() => {
window.removeEventListener('hero-intro-done', tryInit)
tryInit()
}, 3000) }, 3000)
} }
}) })
@@ -156,6 +154,7 @@ onMounted(async () => {
onUnmounted(() => { onUnmounted(() => {
ctx?.revert() ctx?.revert()
ctx = null ctx = null
inited = false
}) })
</script> </script>
@@ -220,7 +219,7 @@ onUnmounted(() => {
class="h-full w-full object-cover" class="h-full w-full object-cover"
:src="item.image || ''" :src="item.image || ''"
:alt="locText(item.titleZh, item.titleEn)" :alt="locText(item.titleZh, item.titleEn)"
loading="lazy" loading="eager"
decoding="async" decoding="async"
@load="refresh" @load="refresh"
/> />