GSAP ScrollTrigger examples

Five scroll animations you can copy today: reveals, parallax, a pinned section, horizontal scrolling and a progress bar. Each one is plain JavaScript and runs as written.

ScrollTrigger ties a GSAP animation to the scroll position. You tell it which element to watch and where on screen to start, and it plays, scrubs or pins the animation for you. Every example below was tested with GSAP 3.15. ScrollTrigger and every other GSAP plugin have been free since version 3.13.

Set it up

Add GSAP and ScrollTrigger from the CDN, just before your closing </body> tag, then register the plugin once.

example.html
<script src="https://cdn.jsdelivr.net/npm/gsap@3.15/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.15/dist/ScrollTrigger.min.js"></script>
<script>
  gsap.registerPlugin(ScrollTrigger);
</script>

Using npm instead? Install gsap, then import and register it.

example.js
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";

gsap.registerPlugin(ScrollTrigger);

1. Fade elements up as they scroll in

The most common scroll animation. ScrollTrigger.batch groups elements that enter together, so a row of cards staggers in rather than popping one by one.

example.js
gsap.set(".reveal", { autoAlpha: 0, y: 40 });

ScrollTrigger.batch(".reveal", {
  start: "top 85%",
  once: true,
  onEnter: (elements) =>
    gsap.to(elements, { autoAlpha: 1, y: 0, duration: 0.8, stagger: 0.1, ease: "power3.out" }),
});

start: "top 85%" means "when the top of the element reaches 85% of the way down the screen". once: true plays it a single time, so scrolling back up doesn't hide the content again.

2. Parallax an image

Move an image slower than the page by scrubbing it against the scroll. The image is a little taller than its frame so the movement never shows an edge.

example.css
.parallax { overflow: hidden; }
.parallax img { width: 100%; height: 120%; object-fit: cover; }
example.js
gsap.to(".parallax img", {
  yPercent: -20,
  ease: "none",
  scrollTrigger: {
    trigger: ".parallax",
    start: "top bottom",
    end: "bottom top",
    scrub: true,
  },
});

scrub: true links the animation to the scrollbar, so it moves forwards and backwards with you. Use ease: "none" on scrubbed animations, or the speed changes oddly mid-scroll.

3. Pin a section and play a timeline

Pinning holds a section in place while you scroll through a timeline. end: "+=150%" keeps it pinned for one and a half screen heights of scrolling.

example.js
const tl = gsap.timeline({
  scrollTrigger: {
    trigger: ".pin",
    start: "top top",
    end: "+=150%",
    pin: true,
    scrub: 1,
  },
});

tl.from(".pin h2", { scale: 0.6, autoAlpha: 0 })
  .to(".pin h2", { xPercent: 50, rotate: 8 });

A number for scrub adds smoothing: scrub: 1 takes a second to catch up with the scrollbar, which feels softer than true.

4. Scroll a row sideways

Pin a section and slide its track left by exactly the width that overflows the screen. Functional values recalculate on resize thanks to invalidateOnRefresh.

example.css
.h-section { overflow: hidden; }
.h-track { display: flex; width: max-content; }
example.js
const track = document.querySelector(".h-track");
const distance = () => track.scrollWidth - window.innerWidth;

gsap.to(track, {
  x: () => -distance(),
  ease: "none",
  scrollTrigger: {
    trigger: ".h-section",
    start: "top top",
    end: () => "+=" + distance(),
    pin: true,
    scrub: 1,
    invalidateOnRefresh: true,
  },
});

5. Show reading progress

A bar across the top of the page that fills as you scroll. With no trigger, ScrollTrigger uses the whole page, and end: "max" means the very bottom.

example.css
.progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  transform: scaleX(0);
  transform-origin: 0 50%;
}
example.js
gsap.to(".progress", {
  scaleX: 1,
  ease: "none",
  scrollTrigger: { start: 0, end: "max", scrub: 0.3 },
});

Respect reduced motion

Some people turn motion off in their system settings. Wrap your scroll animations in gsap.matchMedia() so they only run when motion is welcome. When the setting is on, GSAP reverts everything inside, so hidden elements show again.

example.js
const mm = gsap.matchMedia();

mm.add("(prefers-reduced-motion: no-preference)", () => {
  // put the examples above in here
});

Using React

Install @gsap/react alongside gsap and use the useGSAP hook. It cleans up your animations and ScrollTriggers when the component unmounts, which stops pins and triggers piling up as you change pages.

Example.tsx
"use client";
import { useRef } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useGSAP } from "@gsap/react";

gsap.registerPlugin(ScrollTrigger, useGSAP);

export function Reveal({ children }: { children: React.ReactNode }) {
  const ref = useRef<HTMLDivElement>(null);

  useGSAP(
    () => {
      gsap.from(ref.current, {
        autoAlpha: 0,
        y: 40,
        duration: 0.8,
        ease: "power3.out",
        scrollTrigger: { trigger: ref.current, start: "top 85%", once: true },
      });
    },
    { scope: ref },
  );

  return <div ref={ref}>{children}</div>;
}

Common mistakes

  • Start and end in the wrong place. Add markers: true to any scrollTrigger while you build. It draws the start and end lines on the page. Remove it before you ship.
  • Positions are off after content loads. ScrollTrigger measures the page on load and on resize. Lazy images and embeds that arrive later push things down. Give images a width and height, or call ScrollTrigger.refresh() once the late content is in.
  • A ScrollTrigger inside a timeline's tweens. Put the scrollTrigger on the timeline itself, not on the tweens inside it.
  • Animating the pinned element's position. Pin a wrapper and animate what's inside it, or the pin and your animation fight over the same transform.

Skip the build.

Finished scroll components from the Vault, in HTML and React, with reduced motion and cleanup already in.

Start with 15 free.

Make a free account and take the code for 15 components. No card needed.