Files
arcticnewone/app/plugins/lenis.client.ts
T
陈春 c9cf644ac5 chore: 添加构建推送脚本并更新项目配置
- 添加 build-and-push.sh 脚本用于自动化构建和推送 Docker 镜像
- 更新 .dockerignore 排除 pnpm-workspace.yaml
- 更新项目依赖和配置文件
2026-06-03 17:19:14 +08:00

47 lines
1.2 KiB
TypeScript
Executable File

import Lenis from 'lenis'
import 'lenis/dist/lenis.css'
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
export default defineNuxtPlugin(() => {
// 用 Lenis 平滑浏览器原始 wheel 输入,避免鼠标滚轮一格跳太远。
const lenis = new Lenis({
lerp: 0.075,
wheelMultiplier: 0.7,
})
const updateLenis = (time: number) => {
lenis.raf(time * 1000)
}
// ScrollTrigger 必须跟随 Lenis 的平滑滚动值更新,否则触发点会和真实视觉滚动不同步。
lenis.on('scroll', ScrollTrigger.update)
gsap.ticker.add(updateLenis)
gsap.ticker.lagSmoothing(0)
let isDestroyed = false
const cleanupLenis = () => {
if (isDestroyed) {
return
}
isDestroyed = true
lenis.off('scroll', ScrollTrigger.update)
gsap.ticker.remove(updateLenis)
window.removeEventListener('beforeunload', cleanupLenis)
lenis.destroy()
}
// Nuxt 没有类型化的 app:beforeUnmount;这里用浏览器卸载和 Vite 热更新做清理。
window.addEventListener('beforeunload', cleanupLenis)
import.meta.hot?.dispose(cleanupLenis)
return {
provide: {
lenis,
},
}
})