Pinned Horizontal Scroll in Next.js with GSAP
Scroll down, move sideways: a pinned horizontal section with ScrollTrigger that measures itself correctly, falls back to a normal stack on phones, and animates the panels inside it.
A horizontal scroll section turns the page sideways for a while: the section pins to the screen, and scrolling down slides a row of panels from right to left until the last one is in view. It is one of the most requested effects for portfolios and product stories, and it only takes one tween, once you measure the right things.
How it works
The panels sit in a single row (the track) that is wider than the screen. A ScrollTrigger pins the section and maps the vertical scroll distance onto the track's x position. The distance to travel is the track's width minus the screen's width, and the pin lasts exactly that long, so one pixel of scroll moves the track one pixel.
The component
"use client";
import { useRef, type ReactNode } from "react";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useGSAP } from "@gsap/react";
gsap.registerPlugin(useGSAP, ScrollTrigger);
export function HorizontalScroll({ children }: { children: ReactNode }) {
const section = useRef<HTMLElement>(null);
const track = useRef<HTMLDivElement>(null);
useGSAP(
() => {
const mm = gsap.matchMedia();
// Sideways on larger screens; phones keep a normal vertical stack.
mm.add("(min-width: 768px)", () => {
const distance = () => track.current!.scrollWidth - window.innerWidth;
gsap.to(track.current, {
x: () => -distance(),
ease: "none",
scrollTrigger: {
trigger: section.current,
start: "top top",
end: () => "+=" + distance(),
pin: true,
scrub: 1,
invalidateOnRefresh: true,
},
});
});
},
{ scope: section },
);
return (
<section ref={section} className="overflow-hidden">
<div ref={track} className="flex flex-col gap-8 p-8 md:h-screen md:w-max md:flex-row md:items-center">
{children}
</div>
</section>
);
}Give each panel a width, for example md:w-[70vw], and drop as many as you like inside.
Why the function values
x and end are functions, not numbers, and invalidateOnRefresh is on. ScrollTrigger recalculates on every refresh (a resize, or a call to ScrollTrigger.refresh()), and with functions it measures the track again instead of reusing the width from the first load. Without them, the section ends too early or too late as soon as someone resizes the window.
ease: "none" keeps the movement locked to the scrollbar, and scrub: 1 adds a second of smoothing so it glides rather than ticks.
Phones get a normal stack
Sideways scrolling on a phone fights the thumb, and pinned sections are heavy on small screens. gsap.matchMedia() only builds the animation when the query matches, and reverts it (pin, transforms and all) when it stops matching, so turning a tablet or resizing a window switches cleanly between the two layouts. It is created inside useGSAP, so it is cleaned up on unmount too.
Animating the panels themselves
Normal ScrollTriggers watch vertical scrolling, but inside this section things move sideways. Pass the main tween as containerAnimation and a trigger fires when its element crosses the screen horizontally:
const slide = gsap.to(track.current, { /* the tween above */ });
gsap.utils.toArray<HTMLElement>(".panel img").forEach((img) => {
gsap.from(img, {
scale: 1.3,
ease: "none",
scrollTrigger: {
trigger: img,
containerAnimation: slide,
start: "left right",
end: "right left",
scrub: true,
},
});
});That gives each image a slow parallax zoom as it travels across the screen. The container tween must use ease: "none" for containerAnimation to work, which it already does.
Common problems
- The section ends in the wrong place. Images without a width and height load late and change the track's width after ScrollTrigger measured it. Give them dimensions or an aspect ratio.
- Pinning jumps or drifts. A parent with a
transformorfilterbreaksposition: fixed. See our ScrollTrigger troubleshooting guide. - It stutters with smooth scrolling. Lenis and ScrollTrigger need to share one clock: here is the setup.
- A horizontal scrollbar appears. Keep
overflow-hiddenon the section, so the wide track never widens the page.
Ready-made versions
If you'd rather start from something finished, the Horizontal Scroll Gallery adds Lenis and a fullscreen project view, and the Pinned Lookbook adds velocity-driven marquee type. Both have live previews in the library.