Files
arcticnewone/app/components/home/HomePartnersWork.vue
T
virtheart 639bdcee93 fix: Work 动画最终修复——pinType transform + hydration 后初始化
- 根因1:position:fixed 的 pin 在当前环境失效(元素保持 static),
  改为 pinType:'transform'(transform 固定),浏览器实测 pin 生效、卡片堆叠滑动正常
- 根因2:watch immediate 在 hydration 前初始化导致样式被重置,改到 onMounted
- 卡片定位用原生 style + DOM 选择器 + 自愈重试 + initedOnce
- 本地浏览器逐帧验证:pinTop 恒 0,卡片逐张滑入堆叠(缩放退后/前台/进入)
2026-08-06 23:29:29 +08:00

272 lines
11 KiB
Vue
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onMounted, onUnmounted, ref, computed, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
const { t, locale } = useI18n()
interface WorkItem {
slug: string
titleZh: string
titleEn: string
year: string
image: string | null
}
// 内容 API(仅已发布)
const { data } = await useFetch<{ works: WorkItem[] }>('/api/work')
const works = computed(() => data.value?.works ?? [])
const locText = (zh: string, en: string) => (locale.value === 'zh' ? zh : en)
const rootRef = ref<HTMLElement | null>(null)
const workSectionRef = ref<HTMLElement | null>(null)
const workPinRef = ref<HTMLElement | null>(null)
const partners = [
{
id: 'p1',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M20 44V20h12c7 0 12 4 12 10s-5 10-12 10h-6v4H20Z" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M34 40l10 12" stroke="currentColor" stroke-width="3" stroke-linecap="round"/></svg>',
},
{
id: 'p2',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M14 32c0-9.94 8.06-18 18-18s18 8.06 18 18-8.06 18-18 18-18-8.06-18-18Z" stroke="currentColor" stroke-width="3"/><path d="M24 32c0-4.42 3.58-8 8-8s8 3.58 8 8-3.58 8-8 8-8-3.58-8-8Z" stroke="currentColor" stroke-width="3"/></svg>',
},
{
id: 'p3',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M32 14v10M32 40v10M14 32h10M40 32h10" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M32 22c5.52 0 10 4.48 10 10s-4.48 10-10 10-10-4.48-10-10 4.48-10 10-10Z" stroke="currentColor" stroke-width="3"/></svg>',
},
{
id: 'p4',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M14 44c10-2 12-14 18-18s10-1 18-6" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M16 34c8-2 10 8 18 6s10-18 18-18" stroke="currentColor" stroke-width="3" stroke-linecap="round"/></svg>',
},
{
id: 'p5',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M18 38c0-9 6-18 14-18s14 9 14 18" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M22 42h20" stroke="currentColor" stroke-width="3" stroke-linecap="round"/></svg>',
},
{
id: 'p6',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M18 20v24M30 20v24M42 20v24" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M14 20h36M14 44h36" stroke="currentColor" stroke-width="3" stroke-linecap="round"/></svg>',
},
{
id: 'p7',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M18 44V20h10c9 0 18 7 18 12s-9 12-18 12H18Z" stroke="currentColor" stroke-width="3" stroke-linejoin="round"/></svg>',
},
{
id: 'p8',
svg: '<svg viewBox="0 0 64 64" width="34" height="34" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M16 34c8-14 18-14 32-4" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><path d="M18 44c10-10 20-10 30-2" stroke="currentColor" stroke-width="3" stroke-linecap="round"/></svg>',
},
]
let st: ScrollTrigger | null = null
let madeTweens: gsap.core.Animation[] = []
let positionedCards: HTMLElement[] = []
let initedOnce = false
let retryTimer: ReturnType<typeof setTimeout> | null = null
let stopRetry = false
const refresh = () => {
ScrollTrigger.refresh()
}
// 全部用 DOM 选择器(仅客户端;SSR 无 document,返回空)
const getWorkSection = () => (typeof document !== 'undefined' ? document.getElementById('work') : null)
const getPinEl = () => (typeof document !== 'undefined' ? document.querySelector<HTMLElement>('#work [data-work-pin]') : null)
const getCards = (): HTMLElement[] =>
typeof document !== 'undefined'
? Array.from(document.querySelectorAll<HTMLElement>('#work a[href^="/work/"]'))
: []
const initScrollAnimation = (): boolean => {
if (initedOnce) return true // 只初始化一次,绝不重初始化
const section = getWorkSection()
const pinEl = getPinEl()
if (!section || !pinEl) return false
const cards = getCards()
const count = cards.length
if (count < 2) return false
initedOnce = true
const enterOffset = 160
const scaleTo = 0.92
const depthOffsetY = -18
// 定位卡片为 absolute 堆叠(用原生样式,避免 gsap.set 被 pin 机制 revert)
positionedCards = cards
cards.forEach((card, i) => {
card.style.position = 'absolute'
card.style.top = '0'
card.style.right = '0'
card.style.bottom = '0'
card.style.left = '0'
card.style.zIndex = String(i + 1)
card.style.borderRadius = '22px'
card.style.overflow = 'hidden'
card.style.boxShadow = '0 18px 40px rgba(0,0,0,0.12)'
card.style.transform = i === 0 ? 'none' : `translateY(${window.innerHeight + enterOffset}px)`
})
const tl = gsap.timeline()
for (let i = 1; i < count; i += 1) {
const prev = cards[i - 1]
const next = cards[i]
const start = i - 1
tl.to(next, { 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)
}
madeTweens.push(tl)
st = ScrollTrigger.create({
trigger: section,
start: 'top top',
end: () => `+=${(count - 1) * window.innerHeight}`,
pin: pinEl,
pinType: 'transform',
scrub: true,
anticipatePin: 1,
invalidateOnRefresh: true,
animation: tl,
})
requestAnimationFrame(() => ScrollTrigger.refresh())
return true
}
// 自愈初始化:卡片尚未就绪时每 200ms 重试,直到成功(成功即停)
const ensureInit = () => {
if (stopRetry) return
if (typeof window === 'undefined') return // SSR 不执行
if (initScrollAnimation()) {
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
return
}
retryTimer = setTimeout(ensureInit, 200)
}
// 数据变化时触发(不在 setup 立即执行——hydration 前初始化会被 hydration 重置样式)
watch(works, ensureInit)
onMounted(() => {
ensureInit() // hydration 完成后初始化
if (typeof window !== 'undefined') {
window.addEventListener('hero-intro-done', ensureInit, { once: true })
setTimeout(ensureInit, 3000)
}
})
onUnmounted(() => {
stopRetry = true
if (retryTimer) clearTimeout(retryTimer)
if (st) {
st.kill()
st = null
}
madeTweens.forEach((t) => t.revert())
madeTweens = []
// 复位卡片定位样式
positionedCards.forEach((c) => {
if (!c) return
c.style.position = ''
c.style.top = ''
c.style.right = ''
c.style.bottom = ''
c.style.left = ''
c.style.zIndex = ''
c.style.borderRadius = ''
c.style.overflow = ''
c.style.boxShadow = ''
c.style.transform = ''
})
positionedCards = []
})
</script>
<template>
<section ref="rootRef" class="bg-[#F5F5F5]">
<div class="w-page-container max-w-[calc(100%-2rem)] mx-auto px-6 md:px-10 lg:px-14 pt-24 pb-20">
<div class="grid grid-cols-[1fr_auto_1fr] items-center">
<p class="justify-self-start text-xs font-semibold tracking-tight text-black">( {{ t('home.partners.label') }} )</p>
<div class="flex items-center justify-self-center gap-8 text-xs font-semibold tracking-tight text-black">
<span class="h-1 w-1 rounded-full bg-black" />
<span>{{ t('home.partners.years') }}</span>
</div>
<span class="justify-self-end h-1 w-1 rounded-full bg-black" />
</div>
<div class="mt-12 grid grid-cols-2 sm:grid-cols-4 gap-5">
<div
v-for="item in partners"
:key="item.id"
class="h-[152px] rounded-[20px] bg-white flex items-center justify-center text-black border-0 shadow-none outline-none ring-0"
v-html="item.svg"
/>
</div>
</div>
<section id="work" ref="workSectionRef" class="bg-white">
<div class="w-page-container max-w-[calc(100%-2rem)] mx-auto px-4 pb-28">
<div
data-work-pin
class="flex flex-col lg:flex-row gap-10 lg:gap-16 h-[100svh] items-stretch"
>
<div class="lg:w-[340px] flex flex-col gap-8 pt-8 md:pt-10">
<p class="text-xs font-semibold tracking-tight text-black/70">( {{ t('home.work.label') }} )</p>
<div>
<h2 class="text-5xl md:text-6xl font-bold tracking-tighter leading-[0.92]">
{{ t('home.work.title') }}
<span class="text-black/40 text-base md:text-lg align-middle ml-2 font-semibold tracking-tight">
({{ works.length }})
</span>
</h2>
</div>
<NuxtLink
to="/work"
class="inline-flex w-fit items-center gap-2 rounded-full bg-black px-6 py-3 text-white text-sm font-semibold shadow-[0_15px_30px_-5px_rgba(0,0,0,0.25)]"
>
{{ t('home.work.viewAll') }}
</NuxtLink>
</div>
<div class="relative flex-1 h-full flex items-center">
<div class="relative w-full h-[78svh] md:h-[82svh] lg:h-[88svh] p-[clamp(14px,2.4vw,28px)]">
<div class="relative h-full w-full">
<NuxtLink
v-for="item in works"
:key="item.slug"
:to="`/work/${item.slug}`"
class="will-change-transform relative block"
>
<img
class="h-full w-full object-cover"
:src="item.image || ''"
:alt="locText(item.titleZh, item.titleEn)"
loading="eager"
decoding="async"
@load="refresh"
/>
<div class="absolute left-6 top-6 flex items-baseline gap-3">
<p class="text-white text-sm md:text-base font-semibold tracking-tight drop-shadow-[0_10px_18px_rgba(0,0,0,0.4)]">
{{ locText(item.titleZh, item.titleEn) }}
</p>
<p class="text-white/80 text-xs md:text-sm font-semibold tracking-tight drop-shadow-[0_10px_18px_rgba(0,0,0,0.4)]">
{{ item.year }} ©
</p>
</div>
</NuxtLink>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
</template>