implement_lenis_scroll by jerrar670/surf-website
npx skills add https://github.com/jerrar670/surf-website --skill implement_lenis_scroll此技能将指导您使用 @studio-freight/lenis(或较新的 lenis 包)为 React/Next.js 应用程序添加丝滑平滑的滚动效果。
安装 Lenis 包。
npm install lenis
# 或者根据版本偏好
npm install @studio-freight/lenis
最好将 Lenis 封装在一个可复用的组件或 Hook 中。
选项 A:全局组件(推荐用于 Next.js App Router) 创建 src/lib/lenis.ts 或 src/components/SmoothScroll.tsx。
// src/components/SmoothScroll.tsx
'use client'
import { ReactNode, useEffect } from 'react'
import Lenis from 'lenis'
export default function SmoothScroll({ children }: { children: ReactNode }) {
useEffect(() => {
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
orientation: 'vertical',
gestureOrientation: 'vertical',
smoothWheel: true,
})
function raf(time: number) {
lenis.raf(time)
requestAnimationFrame(raf)
}
requestAnimationFrame(raf)
return () => {
lenis.destroy()
}
}, [])
return <>{children}</>
}
This skill guides you through adding buttery smooth scrolling to a React/Next.js application using @studio-freight/lenis (or the newer lenis package).
Install the Lenis package.
npm install lenis
# OR depending on version preference
npm install @studio-freight/lenis
It's best to encapsulate Lenis in a reusable component or hook.
Option A: Global Component (Recommended for Next.js App Router) Create src/lib/lenis.ts or src/components/SmoothScroll.tsx.
// src/components/SmoothScroll.tsx
'use client'
import { ReactNode, useEffect } from 'react'
import Lenis from 'lenis'
export default function SmoothScroll({ children }: { children: ReactNode }) {
useEffect(() => {
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
orientation: 'vertical',
gestureOrientation: 'vertical',
smoothWheel: true,
})
function raf(time: number) {
lenis.raf(time)
requestAnimationFrame(raf)
}
requestAnimationFrame(raf)
return () => {
lenis.destroy()
}
}, [])
return <>{children}</>
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用该组件包裹您的主要应用程序或布局内容。
Next.js App Router (layout.tsx):
import SmoothScroll from '@/components/SmoothScroll'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<SmoothScroll>
{children}
</SmoothScroll>
</body>
</html>
)
}
性能:确保 requestAnimationFrame 已优化。
滚动恢复:Lenis 通常会处理此问题,但请验证 scrollRestoration 在 Next.js 中是否按预期工作。
GSAP 集成:如果使用 ScrollTrigger,请确保在 Lenis 滚动时更新 ScrollTrigger。
// 在 useEffect 内部
// lenis.on('scroll', ScrollTrigger.update) // gsap.ticker.add((time)=>{lenis.raf(time * 1000)}) // gsap.ticker.lagSmoothing(0)
每周安装量
74
代码仓库
首次出现
2026年1月23日
已安装于
opencode63
codex62
gemini-cli60
github-copilot57
cursor55
kimi-cli52
Wrap your main application or layout content with the component.
Next.js App Router (layout.tsx):
import SmoothScroll from '@/components/SmoothScroll'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<SmoothScroll>
{children}
</SmoothScroll>
</body>
</html>
)
}
Performance : Ensure requestAnimationFrame is optimized.
Scroll Restoration : Lenis usually handles this, but verify scrollRestoration works as expected in Next.js.
GSAP Integration : If using ScrollTrigger, ensure you update ScrollTrigger on Lenis scroll.
// Inside useEffect
// lenis.on('scroll', ScrollTrigger.update) // gsap.ticker.add((time)=>{lenis.raf(time * 1000)}) // gsap.ticker.lagSmoothing(0)
Weekly Installs
74
Repository
First Seen
Jan 23, 2026
Installed on
opencode63
codex62
gemini-cli60
github-copilot57
cursor55
kimi-cli52
Genkit JS 开发指南:AI 应用构建、错误排查与最佳实践
7,700 周安装