Files
gsap-skills/skills/gsap-scrolltrigger/SKILL.md
T
2026-03-04 15:14:02 -06:00

4.2 KiB
Raw Blame History

name, description, license
name description license
gsap-scrolltrigger Use ScrollTrigger for scroll-linked animations, pinning, scrub, and triggers. Use when building scroll-based animations, parallax, pinned sections, or when the user asks about ScrollTrigger, scroll animations, or pinning with GSAP. MIT

GSAP ScrollTrigger

When to Use This Skill

Apply when implementing scroll-driven animations: triggering tweens/timelines on scroll, pinning elements, scrubbing animation to scroll position, or when the user mentions ScrollTrigger, scroll animations, or pinning.

Registering the Plugin

ScrollTrigger is a plugin. After loading the script, register it once:

gsap.registerPlugin(ScrollTrigger);

Basic Trigger

Tie a tween or timeline to scroll position:

gsap.to(".box", {
  x: 500,
  duration: 1,
  scrollTrigger: {
    trigger: ".box",
    start: "top center",   // when top of trigger hits center of viewport
    end: "bottom center",  // when the bottom of the trigger hits the center of the viewport
    toggleActions: "play reverse play reverse" // onEnter play, onLeave reverse, onEnterBack play, onLeaveBack reverse
  }
});

start / end: viewport position vs. trigger position. Format "triggerPosition viewportPosition". Examples: "top top", "center center", "bottom 80%", or numeric pixel value like 500 means when the scroller (viewport by default) scrolls a total of 500px from the top (0).

Scrub

Scrub ties animation progress to scroll. Use for “scroll-driven” feel:

gsap.to(".box", {
  x: 500,
  scrollTrigger: {
    trigger: ".box",
    start: "top center",
    end: "bottom center",
    scrub: true        // or number (smoothness delay in seconds), so 0.5 means it'd take 0.5 seconds to "catch up" to the current scroll position.
  }
});

With scrub: true, the animation progresses as the user scrolls through the start–end range. Use a number (e.g. scrub: 1) for smooth lag.

Pinning

Pin the trigger element while the scroll range is active:

scrollTrigger: {
  trigger: ".section",
  start: "top top",
  end: "+=1000",   // pin for 1000px scroll
  pin: true,
  scrub: 1
}
  • pinSpacing — default true; adds spacer element so layout doesn’t collapse when the pinned element is set to position: fixed. Set pinSpacing: false only when layout is handled separately.

Markers (Development)

Use during development to see trigger positions:

scrollTrigger: {
  trigger: ".box",
  start: "top center",
  end: "bottom center",
  markers: true
}

Remove or set markers: false for production.

Timeline + ScrollTrigger

Drive a timeline with scroll and optional scrub:

const tl = gsap.timeline({
  scrollTrigger: {
    trigger: ".container",
    start: "top top",
    end: "+=2000",
    scrub: 1,
    pin: true
  }
});
tl.to(".a", { x: 100 }).to(".b", { y: 50 }).to(".c", { opacity: 0 });

The timeline’s progress is tied to scroll through the trigger’s start/end range.

Refresh and Cleanup

  • ScrollTrigger.refresh() — recalculate positions (e.g. after DOM/layout changes, fonts loaded, or dynamic content).
  • When removing animated elements or changing pages (e.g. in SPAs), kill associated ScrollTrigger instances so they don’t run on stale elements:
ScrollTrigger.getAll().forEach(t => t.kill());
// or kill by trigger element
ScrollTrigger.getById("myTrigger")?.kill();

In React, kill in a cleanup (e.g. in useEffect return) when the component unmounts.

Do Not

  • Nest ScrollTriggered animations inside of a parent timeline. ScrollTriggers should only exist on top-level animations. Wrong: gsap.timeline().to(..., {scrollTrigger: {...}}). Correct: gsap.timeline({scrollTrigger: {...}}).to(...) (the tween inside the timeline shouldn't have a ScrollTrigger).
  • Forget to gsap.registerPlugin(ScrollTrigger) before using ScrollTrigger.
  • Use scrub and toggleActions together on the same ScrollTrigger; choose one behavior. If both exist, scrub wins.
  • Leave markers: true in production.
  • Omit refresh() after layout changes (new content, images, fonts) that affect trigger positions. ScrollTrigger will automatically call ScrollTrigger.refresh() when the viewport resizes.