implement_lenis_scroll
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImplement Lenis Smooth Scroll
实现Lenis平滑滚动
This skill guides you through adding buttery smooth scrolling to a React/Next.js application using (or the newer package).
@studio-freight/lenislenis本指南将引导你使用(或更新的包)在React/Next.js应用中添加丝滑的滚动效果。
@studio-freight/lenislenisPrerequisites
前提条件
- A React or Next.js project.
- 一个React或Next.js项目。
Steps
步骤
1. Install Dependencies
1. 安装依赖
Install the Lenis package.
bash
npm install lenis安装Lenis包。
bash
npm install lenisOR depending on version preference
OR depending on version preference
npm install @studio-freight/lenis
undefinednpm install @studio-freight/lenis
undefined2. Create the Lenis Hook/Component
2. 创建Lenis钩子/组件
It's best to encapsulate Lenis in a reusable component or hook.
Option A: Global Component (Recommended for Next.js App Router)
Create or .
src/lib/lenis.tssrc/components/SmoothScroll.tsxtypescript
// 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}</>
}最好将Lenis封装在可复用的组件或钩子中。
选项A:全局组件(推荐用于Next.js App Router)
创建或。
src/lib/lenis.tssrc/components/SmoothScroll.tsxtypescript
// 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}</>
}3. Integrate into Layout
3. 集成到布局中
Wrap your main application or layout content with the component.
Next.js App Router ():
layout.tsxtypescript
import SmoothScroll from '@/components/SmoothScroll'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<SmoothScroll>
{children}
</SmoothScroll>
</body>
</html>
)
}将主应用或布局内容用该组件包裹。
Next.js App Router():
layout.tsxtypescript
import SmoothScroll from '@/components/SmoothScroll'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<SmoothScroll>
{children}
</SmoothScroll>
</body>
</html>
)
}Best Practices
最佳实践
- Performance: Ensure is optimized.
requestAnimationFrame - Scroll Restoration: Lenis usually handles this, but verify works as expected in Next.js.
scrollRestoration - GSAP Integration: If using ScrollTrigger, ensure you update ScrollTrigger on Lenis scroll.
typescript
// Inside useEffect // lenis.on('scroll', ScrollTrigger.update) // gsap.ticker.add((time)=>{lenis.raf(time * 1000)}) // gsap.ticker.lagSmoothing(0)
- 性能:确保已优化。
requestAnimationFrame - 滚动恢复:Lenis通常会处理此问题,但需验证Next.js中的是否按预期工作。
scrollRestoration - GSAP集成:如果使用ScrollTrigger,请确保在Lenis滚动时更新ScrollTrigger。
typescript
// Inside useEffect // lenis.on('scroll', ScrollTrigger.update) // gsap.ticker.add((time)=>{lenis.raf(time * 1000)}) // gsap.ticker.lagSmoothing(0)