A pinned section with a scrubbed timeline holds a panel still while the visitor scrolls, and plays the animation forward and back with the scrollbar. In React the trouble is rarely the animation. It's the page around it: content that jumps when the pin starts, positions that go stale after fonts load or the route changes, and doubled triggers in development. This guide builds one small component with GSAP's ScrollTrigger and useGSAP, and the demo below runs exactly the code shown further down.
The demo
Scroll down through the dark panel. It pins in the middle of the viewport, the bar fills and the three steps slide in as you scroll, and scrolling back up runs it in reverse. Every step's text is in the page from the start, so nothing is said only through motion. With reduced motion turned on there's no pin and no scrub: the panel shows its finished state and scrolls like the rest of the page.
Scroll to scrub the timelineReduced motion: every step is shown, no pin
- Pin the wrapperThe section holds still while the page keeps scrolling.
- Scrub the timelineScroll position drives the playhead, forward and back.
- Release cleanlyPin spacing already made room, so nothing below jumps.
Copy these components for free
Create a free Motionbits account to copy the source of every free component, light, dark and reduced-motion variants included. Free forever, no card required.
Create a free accountRegister ScrollTrigger once
ScrollTrigger ships inside the gsap package but isn't part of the core, so import it from gsap/ScrollTrigger and register it next to the hook: gsap.registerPlugin(useGSAP, ScrollTrigger), once, at the top of the module (ScrollTrigger docs). The examples here use gsap 3.15 and @gsap/react 2.1. Everything in the file touches the DOM, so in the Next.js App Router it starts with "use client". useGSAP is safe with server rendering in a client component: it prefers useLayoutEffect and falls back to useEffect when there's no window (GSAP's React guide).
useGSAP and scope: cleanup that survives Strict Mode
Every animation and ScrollTrigger created while the useGSAP function runs is recorded in a gsap.context() and reverted when the component unmounts. For a pin, reverting matters more than usual: killing a ScrollTrigger unpins the element and undoes the DOM changes the pin made, including the spacer wrapped around it. { scope: section } limits selector text such as ".pin-step" to this component's own elements.
In development, React Strict Mode runs each effect through an extra setup and cleanup cycle. A pin built in a plain useEffect with no cleanup ends up created twice, with two triggers fighting over the same element. With useGSAP, the first run is reverted before the second starts, so you get one pin. That's the same hook the GSAP timelines guide builds on.
A pinned section with a scrubbed timeline
"use client";
import { useRef } from "react";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useGSAP } from "@gsap/react";
gsap.registerPlugin(useGSAP, ScrollTrigger);
const STEPS = [
{ title: "Pin the wrapper", body: "The section holds still while the page keeps scrolling." },
{ title: "Scrub the timeline", body: "Scroll position drives the playhead, forward and back." },
{ title: "Release cleanly", body: "Pin spacing already made room, so nothing below jumps." },
];
export function PinScrub() {
const section = useRef<HTMLDivElement>(null);
const track = useRef<HTMLDivElement>(null);
useGSAP(
() => {
const mm = gsap.matchMedia();
mm.add(
{
wide: "(min-width: 768px) and (prefers-reduced-motion: no-preference)",
narrow: "(max-width: 767px) and (prefers-reduced-motion: no-preference)",
},
(media) => {
const narrow = Boolean(media.conditions?.narrow);
gsap
.timeline({
defaults: { ease: "none" },
scrollTrigger: {
trigger: section.current,
pin: true,
start: "center center",
end: () => `+=${window.innerHeight * (narrow ? 0.8 : 1.2)}`,
scrub: 0.5,
invalidateOnRefresh: true,
},
})
.from(".pin-fill", { scaleX: 0, duration: 3 }, 0)
.from(".pin-head", { x: () => -track.current!.offsetWidth, duration: 3 }, 0)
.from(".pin-step", { x: narrow ? 12 : 24, opacity: 0, duration: 1, stagger: 1 }, 0);
},
);
document.fonts.ready.then(() => ScrollTrigger.refresh());
},
{ scope: section },
);
return (
<div ref={section} className="pin">
<div className="pin-stage">
<p className="pin-label">
<span className="pin-label-motion">Scroll to scrub the timeline</span>
<span className="pin-label-reduced">Reduced motion: every step is shown, no pin</span>
</p>
<div ref={track} className="pin-track" aria-hidden="true">
<div className="pin-fill" />
<div className="pin-head" />
</div>
<ol className="pin-steps">
{STEPS.map((step) => (
<li key={step.title} className="pin-step">
<span className="pin-step-title">{step.title}</span>
<span className="pin-step-body">{step.body}</span>
</li>
))}
</ol>
</div>
</div>
);
}
| Option | What it does here |
|---|---|
trigger: section.current | The wrapper decides where the pin starts and ends. |
pin: true | Pins the trigger element while the ScrollTrigger is active. |
start: "center center" | Pins when the wrapper's center meets the viewport's center. With pin: true the default is "top top", which on this site would tuck the panel under the fixed header. |
end: () => "+=…" | Ends a set distance after the start. As a function, it's recalculated on every refresh. |
scrub: 0.5 | Links the timeline to the scrollbar, catching up over half a second. |
invalidateOnRefresh: true | Re-reads function-based tween values, like the playhead's travel, on refresh. |
scrub: true ties the timeline's progress directly to the scroll position. A number is the time, in seconds, the playhead takes to catch up with the scrollbar, which smooths out choppy wheel input; scrub: 0.5 is a light touch. Either way, durations inside a scrubbed timeline are only proportions: the whole timeline is stretched to fit between start and end. To make it take longer, push end further down rather than raising durations. The tweens use ease: "none" so the scrollbar alone sets the pace, and they move only transform and opacity, in line with the motion rules behind every piece. For easing on non-scrubbed motion, see easing that feels right.
Why pinning makes the page jump, and what pinSpacing does
While it's active, a pin takes the element out of the flow with position: fixed. Left alone, the content below would slide up into the hole, or scroll under the pinned panel. By default ScrollTrigger prevents that with pinSpacing: it wraps the element in a spacer and adds padding equal to the pin's scroll distance, so the following content catches up exactly when the pin releases. pinSpacing: false turns that off, for when you want the next section to scroll over the pinned one, and pinSpacing: "margin" uses margin instead of padding.
- If the pinned element's container is
display: flex, ScrollTrigger setspinSpacingtofalseby default, so the content after it doesn't move down. Pin a block-level wrapper, or space things out yourself. - A
transformorwill-changeon any ancestor breaksposition: fixedin the browser, and the pinned element shifts and then scrolls away. Remove it if you can.pinReparent: truemoves the element to the body while pinned, but GSAP calls it expensive and a last resort, and it breaks CSS selectors that rely on nesting. - If a large pinned panel seems to snap in a frame late when you scroll fast,
anticipatePin: 1applies the pin slightly early. The default is 0, and in many cases you don't need it.
Pin the wrapper, animate what's inside it
ScrollTrigger measures the pinned element up front, so GSAP's docs warn against animating it: moving or scaling it throws off the measurements. The demo pins .pin, the component's outer box, and animates only the bar, the playhead and the steps inside it. If you need the whole panel to move, wrap it in one more element and pin that.
Refresh when the layout changes
ScrollTrigger works out its start and end positions when it's created and again when the window resizes. It doesn't watch the DOM, so anything that changes the layout above a trigger afterward leaves it pinning at the wrong place. Call ScrollTrigger.refresh() after it:
- Web fonts that change line lengths once they load. The demo calls
document.fonts.ready.then(() => ScrollTrigger.refresh()). - Images without
widthandheightattributes, and content you fetch and insert. Refresh in their load callback, for exampleonLoad={() => ScrollTrigger.refresh()}. - Accordions or tabs that expand above the pin.
ScrollTrigger.refresh(true)waits at least one animation frame, and up to about 200 ms, so the browser has rendered the change before ScrollTrigger measures.
On refresh, function-based start and end values are called again. end: "+=" + el.offsetHeight is computed once, when the trigger is created, and goes stale; end: () => "+=" + el.offsetHeight is recomputed every time. The same goes for tween values: the playhead's x: () => -track.current!.offsetWidth only updates because the ScrollTrigger sets invalidateOnRefresh: true, which flushes the timeline's recorded start values on each refresh.
Copy these components for free
Create a free Motionbits account to copy the source of every free component, light, dark and reduced-motion variants included. Free forever, no card required.
Create a free accountMore than one pin on a page
A pin with the default spacing pushes everything below it down, so ScrollTriggers further down the page have to be measured after it. Create them in the order they appear, top to bottom. If you can't, because components mount in a different order, give the earlier ones a higher refreshPriority (the default is 0, and higher refreshes sooner), or reorder them with ScrollTrigger.sort(). Each component can keep its own useGSAP; only the order they're measured in matters.
Markers while you build
markers: true draws the start, end and trigger points on the page, and an object such as { startColor: "white", endColor: "white" } restyles them. They're for development only, so tie them to the environment: markers: process.env.NODE_ENV === "development". Next.js inlines NODE_ENV at build time, so production builds get markers: false.
Reduced motion and small screens with gsap.matchMedia()
gsap.matchMedia() runs a function when a media query matches, and reverts every animation and ScrollTrigger the function created when the query stops matching. The demo's two conditions both include (prefers-reduced-motion: no-preference). When the visitor asks for reduced motion, neither matches, so nothing is created: no pin, no spacer, no scrub, and the markup shows the timeline's end state as plain content. If the setting changes while the page is open, the pin is reverted, or built, on the spot.
Scrubbed motion starts because someone scrolled, so it's covered by WCAG 2.3.3 Animation from Interactions (Level AAA), which asks that non-essential motion triggered by interaction can be turned off, rather than by the auto-play rule behind pausable heroes and marquees. Honoring prefers-reduced-motion is one of its sufficient techniques, and it's how every Motionbits piece behaves; see how each piece handles reduced motion.
The same conditions object handles breakpoints. The demo gives narrow screens a shorter scroll distance and a smaller slide, and with invalidateOnRefresh the function-based values follow resizes within a breakpoint. Crossing 768px reverts the timeline and builds it again with the other values.
Troubleshooting
- The page jumps when the pin starts: check for a
transformorwill-changeon an ancestor, adisplay: flexparent turning offpinSpacing, or a tween on the pinned element itself. A one-frame flash on fast scrolls is whatanticipatePinis for. - Positions are wrong after a route change: let
useGSAPkill the old triggers on unmount and create new ones on mount, thenScrollTrigger.refresh()once the new page's fonts, images or data have settled. Also check that triggers are created top to bottom. - Markers are off after a resize:
scroll-behavior: smoothonhtmldelays the scroll reset ScrollTrigger does while refreshing. GSAP's fix ishtml { scroll-behavior: auto }. - Two pins, or a doubled spacer, in development only: that's Strict Mode running effects twice against a pin with no cleanup. Create it inside
useGSAP, and wrap anything created later, in an event handler, withcontextSafe(). - A scrubbed animation starts part-way through on load: its
startis above the top of the page. Move it lower, or wrap it as"clamp(top bottom)".
Where to go next
Build the sequence first and scroll-link it second: GSAP timelines in React covers the position parameter and contextSafe(). In the Library, Theme Tip and Tumble Tabs are free Motionbits components with live demos, built to the same rules: short, transform and opacity only, and a reduced-motion path. A free account gets you the source of the free components. New releases are part of Pro, which isn't open yet.
Copy these components for free
Create a free Motionbits account to copy the source of every free component, light, dark and reduced-motion variants included. Free forever, no card required.
Create a free accountSources
- GSAP, ScrollTrigger
- GSAP, ScrollTrigger.refresh()
- GSAP, ScrollTrigger tips and mistakes
- GSAP, React and useGSAP
- GSAP, gsap.matchMedia()
- React,
<StrictMode> - W3C WAI, Understanding SC 2.3.3 Animation from Interactions