Scroll animations with GSAP that respect reduced motion
1 min read
On this page
Scroll animations make a page feel alive. Done carelessly, they also make content flash in late, fight the browser's layout and make some people feel ill. Here's the setup this site uses, built on GSAP and its React hook.
Animate only with full motion
gsap.matchMedia() runs a block only while a media query matches, and reverts everything it created
when it stops matching. Wrap every animation in it:
export const FULL_MOTION = "(prefers-reduced-motion: no-preference)";useGSAP(
() => {
const el = ref.current;
gsap.matchMedia().add(FULL_MOTION, () => {
gsap.from(el.children, { autoAlpha: 0, y: 40, stagger: 0.1 });
});
},
{ scope: ref },
);Someone with reduced motion turned on gets the finished page straight away. Nothing to opt out of.
No flash before hydration
The server sends the content visible. The animation hides it in the browser, after a moment where it was already on screen: a flash. The fix is to hide it in CSS, but only for people who will see the animation:
@media (prefers-reduced-motion: no-preference) {
[data-animate] {
visibility: hidden;
}
}Then the animation reveals it with autoAlpha, which sets visibility and opacity together.
Trigger once, on entry
scrollTrigger: { trigger: el, start: "top 85%", once: true }once: true plays the animation the first time the element scrolls in and then removes the
trigger, so scrolling back up doesn't replay it or cost anything.
Wrap it in a component
All of this lives in one <Reveal> component, so a page only writes:
<Reveal className="grid gap-4 md:grid-cols-3">
<Card />
<Card />
<Card />
</Reveal>Server Components can use it too: only <Reveal> itself is a Client Component, and the cards inside
it still render on the server.
- GSAP
- Animation
- Accessibility
