refactor(home): 将首页组件拆分为独立模块并优化代码结构

将首页的加载动画、介绍动画和英雄区域拆分为独立的组件文件
移除index.vue中冗余的样式和逻辑,通过组件引用方式重构
优化代码组织结构,提高可维护性
This commit is contained in:
2026-04-26 23:07:37 +08:00
parent 8b5f6720cd
commit 1bc881994f
4 changed files with 400 additions and 350 deletions
+228
View File
@@ -0,0 +1,228 @@
<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref } from 'vue'
import type Lenis from 'lenis'
import gsap from 'gsap'
const heroPinDistance = 1200
const heroGalleryColumns = [
{
side: 'left',
images: [
'https://picsum.photos/id/1018/900/1200',
'https://picsum.photos/id/1015/900/1200',
'https://picsum.photos/id/1024/900/1200',
'https://picsum.photos/id/1039/900/1200',
],
},
{
side: 'right',
images: [
'https://picsum.photos/id/1043/900/1200',
'https://picsum.photos/id/1050/900/1200',
'https://picsum.photos/id/1067/900/1200',
'https://picsum.photos/id/1074/900/1200',
],
},
] as const
const heroStageRef = ref<HTMLElement | null>(null)
const heroRef = ref<HTMLElement | null>(null)
const heroPanelRef = ref<HTMLElement | null>(null)
let cleanupHeroScroll: (() => void) | null = null
onMounted(() => {
const heroStageElement = heroStageRef.value
const heroPanelElement = heroPanelRef.value
if (!heroStageElement || !heroPanelElement) {
return
}
const { $lenis } = useNuxtApp() as ReturnType<typeof useNuxtApp> & { $lenis?: Lenis }
const pinEndScale = 0.65
const finalScale = 0.18
const getExitDistance = () => window.innerHeight
const setupHeroScroll = () => {
cleanupHeroScroll?.()
gsap.set(heroPanelElement, {
scale: 1,
force3D: true,
transformOrigin: 'center center',
})
let renderedScale = -1
const setHeroPanelScale = (scale: number) => {
if (Math.abs(renderedScale - scale) < 0.001) {
return
}
renderedScale = scale
gsap.set(heroPanelElement, { scale })
}
const updateHeroPanelScale = () => {
const exitDistance = getExitDistance()
const totalDistance = heroPinDistance + exitDistance
// Lenis 存在时只读 Lenis 的平滑滚动值,避免向上滚时 window.scrollY 和 Lenis 值互相打架。
const currentScroll = $lenis ? $lenis.scroll : window.scrollY
const currentDistance = gsap.utils.clamp(0, totalDistance, currentScroll - heroStageElement.offsetTop)
if (currentDistance <= heroPinDistance) {
setHeroPanelScale(gsap.utils.interpolate(1, pinEndScale, currentDistance / heroPinDistance))
return
}
setHeroPanelScale(
gsap.utils.interpolate(
pinEndScale,
finalScale,
(currentDistance - heroPinDistance) / exitDistance,
),
)
}
// CSS sticky 负责固定;ticker 每帧读取当前滚动距离,避免依赖 scroll/ScrollTrigger 事件链。
updateHeroPanelScale()
gsap.ticker.add(updateHeroPanelScale)
cleanupHeroScroll = () => {
gsap.ticker.remove(updateHeroPanelScale)
}
}
setupHeroScroll()
})
onBeforeUnmount(() => {
cleanupHeroScroll?.()
cleanupHeroScroll = null
})
defineExpose({
heroStageRef,
heroRef,
})
</script>
<template>
<section
ref="heroStageRef"
class="hero-stage"
:style="{ '--hero-pin-distance': `${heroPinDistance}px` }"
>
<section ref="heroRef" class="hero-section" v-once>
<div class="hero-backdrop" aria-hidden="true">
<div
v-for="column in heroGalleryColumns"
:key="column.side"
class="hero-gallery"
:class="`hero-gallery-${column.side}`"
>
<!-- 复制两份图片组配合 translateY(-50%) 做无缝循环 -->
<div
class="hero-gallery-track"
:class="{ 'hero-gallery-track-slow': column.side === 'right' }"
>
<div v-for="copy in 2" :key="`${column.side}-copy-${copy}`" class="hero-gallery-group">
<img
v-for="image in column.images"
:key="`${column.side}-${copy}-${image}`"
class="hero-gallery-image"
:src="image"
alt=""
decoding="async"
loading="eager"
/>
</div>
</div>
</div>
</div>
<div ref="heroPanelRef" class="hero-panel">
<p>Hero Section</p>
</div>
</section>
</section>
</template>
<style scoped>
.hero-stage {
position: relative;
height: calc(100svh + var(--hero-pin-distance));
background: #000;
}
.hero-section {
position: sticky;
top: 0;
height: 100svh;
overflow: hidden;
background: #111;
}
.hero-backdrop {
position: absolute;
inset: 0;
overflow: hidden;
background: #000;
}
.hero-gallery {
position: absolute;
top: -40px;
bottom: -40px;
width: 49vw;
overflow: hidden;
}
.hero-gallery-left {
left: 0;
}
.hero-gallery-right {
right: 0;
}
.hero-gallery-track {
will-change: transform;
animation: hero-gallery-scroll 28s linear infinite;
}
.hero-gallery-track-slow {
animation-duration: 42s;
}
.hero-gallery-group {
display: grid;
gap: 36px;
padding-bottom: 36px;
}
.hero-gallery-image {
display: block;
width: 100%;
height: clamp(260px, 42vh, 430px);
border-radius: 0;
object-fit: cover;
}
@keyframes hero-gallery-scroll {
to {
transform: translateY(-50%);
}
}
.hero-panel {
position: absolute;
inset: 0;
display: grid;
place-items: center;
background: #fff;
font-size: 48px;
will-change: transform;
}
</style>
+88
View File
@@ -0,0 +1,88 @@
<script setup lang="ts">
import { ref } from 'vue'
const introTitle = 'ArcticNewOne'
const introLetters = introTitle.split('')
const introRef = ref<HTMLElement | null>(null)
const introTitleRef = ref<HTMLElement | null>(null)
defineExpose({
introRef,
introTitleRef
})
</script>
<template>
<div ref="introRef" class="hero-intro" aria-hidden="true">
<h1 ref="introTitleRef" class="hero-intro-title" :aria-label="introTitle" v-once>
<span class="hero-intro-word">
<span
v-for="(letter, index) in introLetters"
:key="`${letter}-${index}`"
class="hero-intro-char-wrap"
>
<span class="hero-intro-char">{{ letter }}</span>
</span>
<span class="hero-intro-tm">TM</span>
</span>
</h1>
</div>
</template>
<style scoped>
.hero-intro {
position: fixed;
inset: 0;
z-index: 80;
display: grid;
place-items: center;
overflow: hidden;
color: #fff;
background: #000;
opacity: 0;
visibility: hidden;
will-change: transform;
}
.hero-intro-title {
display: flex;
margin: 0;
font-size: clamp(36px, 6.2vw, 104px);
font-weight: 700;
line-height: 0.9;
letter-spacing: -0.07em;
white-space: nowrap;
}
.hero-intro-word {
position: relative;
display: inline-flex;
}
.hero-intro-char-wrap,
.hero-intro-char {
display: inline-block;
}
.hero-intro-char-wrap {
position: relative;
overflow: hidden;
}
.hero-intro-char {
will-change: transform;
}
.hero-intro-tm {
position: absolute;
left: calc(100% + 0.12em);
top: 0.08em;
font-size: 0.13em;
font-weight: 700;
letter-spacing: -0.02em;
line-height: 1;
opacity: 0;
visibility: hidden;
}
</style>
+65
View File
@@ -0,0 +1,65 @@
<script setup lang="ts">
import { ref } from 'vue'
// 定义 loader 相关的 DOM 引用
const loaderRef = ref<HTMLElement | null>(null)
const loaderBarRef = ref<HTMLElement | null>(null)
// 暴露这两个 ref 供外部使用
defineExpose({
loaderRef,
loaderBarRef
})
</script>
<template>
<div ref="loaderRef" class="page-loader" aria-hidden="true" v-once>
<div class="page-loader-content">
<p>Loading</p>
<span class="page-loader-line">
<span ref="loaderBarRef" class="page-loader-line-inner" />
</span>
</div>
</div>
</template>
<style scoped>
.page-loader {
position: fixed;
inset: 0;
z-index: 90;
display: grid;
place-items: center;
color: #fff;
background: #000;
will-change: transform;
}
.page-loader-content {
width: min(220px, calc(100vw - 48px));
font-size: 12px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.page-loader-content p {
margin: 0 0 14px;
}
.page-loader-line {
display: block;
width: 100%;
height: 1px;
overflow: hidden;
background: rgb(255 255 255 / 20%);
}
.page-loader-line-inner {
display: block;
width: 100%;
height: 100%;
background: #fff;
transform: scaleX(0);
transform-origin: left center;
}
</style>