Glide Like a Pro: How I Made Smooth Scrolling Magic in Next.js with Lenis and GSAP
Alright, picture this: itโs 2 AM, my second cup of coffee has gone cold, and Iโm sitting there, trying to figure out why my Next.js app scrolls like a PowerPoint presentation from 2005. (You know the ones, where every transition looks like itโs running on a dial-up connection.)

If youโre into web development, you get how satisfying it is when your app scrolls so smoothly it feels like butter melting on a hot pan. Well, thatโs exactly what I wanted. I didnโt want a scroll; I wanted an experience. And guess what? I found the magic combo: Lenis and GSAP.
Let me break it down for you, step by step, like weโre hanging out and coding together. (Trust me, by the end of this, youโll be adding smooth scrolling to everythingโeven your shopping cart app.)
- See also: How I Nailed Fetching User Location in React.js (Without Losing My Mind ๐ )
The Backstory: Why Smooth Scrolling?
First, letโs talk about why smooth scrolling matters. Sure, itโs one of those “small touches,” but in the world of UX, small touches are what separate meh from chefโs kiss.
When users scroll, you want them to feel like theyโre gliding through your content effortlessly, not like theyโre navigating a laggy mess. And if youโre building anything remotely creativeโportfolios, blogs, or SaaS dashboardsโsmooth scrolling is like adding that perfect Instagram filter.
So, after a lot of Googling (and cursing at janky scroll attempts), I stumbled upon Lenis, a smooth scrolling library thatโs lightweight, performant, and surprisingly easy to work with. Add GSAP into the mix for animations, and youโre golden.
Step 1: Setting Up Your Next.js Project
First things first, you need a Next.js app. If you donโt already have one, fire up your terminal and run:
npx create-next-app@latest smooth-scroll-demo
cd smooth-scroll-demo
npm install
Now youโve got your boilerplate. Open up your favorite code editor (shoutout to VS Code gang ๐), and letโs get cracking.
Step 2: Installing Lenis and GSAP
This is where the magic begins. Install both libraries:
npm install @studio-freight/lenis gsap
For those new to Lenis, think of it as a scroll manager that smooths out the user experience without needing 10,000 lines of code. And GSAP? Itโs the animation wizard thatโll make your scroll transitions pop.
Step 3: Setting Up Lenis
Hereโs the cool thing about Lenis: itโs ridiculously easy to integrate. Create a new file, lenis.js
, inside a folder like /lib
or /utils
(up to you).
import Lenis from '@studio-freight/lenis';
export const initLenis = () => {
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Custom easing curve
smooth: true,
});
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf);
}
requestAnimationFrame(raf);
return lenis;
};
Now, head over to your _app.js
file and initialize Lenis:
import { useEffect } from 'react';
import { initLenis } from '../utils/lenis';
function MyApp({ Component, pageProps }) {
useEffect(() => {
const lenis = initLenis();
return () => lenis.destroy(); // Clean up on unmount
}, []);
return <Component {...pageProps} />;
}
export default MyApp;
At this point, your app should already feel smoother. Take a moment to scroll and soak it in. (No, seriouslyโtake a break. Youโve earned it.)
Step 4: Adding GSAP for Animations
Hereโs where the real fun begins. GSAP makes animations feel so seamless itโs like sprinkling fairy dust on your project. Install the ScrollTrigger plugin:
npm install gsap
In index.js
(or wherever your main content lives), letโs set up a basic animation.
import { useEffect } from 'react';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger);
export default function Home() {
useEffect(() => {
const sections = document.querySelectorAll('.section');
gsap.fromTo(
sections,
{ opacity: 0, y: 50 },
{
opacity: 1,
y: 0,
duration: 1,
ease: 'power2.out',
stagger: 0.2,
scrollTrigger: {
trigger: sections,
start: 'top 80%',
end: 'bottom 20%',
scrub: true,
},
}
);
}, []);
return (
<div>
<div className="section" style={{ height: '100vh', background: '#f3f4f6' }}>
<h1>Welcome to Smooth Scrolling</h1>
</div>
<div className="section" style={{ height: '100vh', background: '#e5e7eb' }}>
<h2>Scroll down to see the magic โจ</h2>
</div>
<div className="section" style={{ height: '100vh', background: '#d1d5db' }}>
<h2>Thatโs all, folks!</h2>
</div>
</div>
);
}
Step 5: Fine-Tuning and Debugging
Hereโs the thingโsmooth scrolling can be a bit extra. Youโll need to tweak values like duration
, scrub
, and easing curves to get it just right.
For me, the sweet spot was a 1.2
duration and a custom easing function (because why not?). Also, donโt forget to test on different devicesโwhat looks amazing on desktop might feel sluggish on mobile.
Pro tip: If your animations feel “off,” check your CSS. Sometimes, overflow or z-index issues can mess with the scroll experience.
Step 6: Celebrate! ๐
Thatโs itโyouโve officially mastered smooth scrolling in Next.js using Lenis and GSAP. Pat yourself on the back, blast your favorite playlist, and marvel at how silky smooth your app feels now.
Final Thoughts
If youโve made it this far, youโre basically a scroll wizard. But donโt stop hereโLenis and GSAP are just the beginning. Experiment with parallax effects, interactive scroll-triggered animations, and more.
And hey, if you run into any issues, drop a comment below. Iโll probably be up at 2 AM again, coffee in hand, ready to help. ๐
Until next time, keep scrolling smooth, my friends. โ๏ธ