ScrollTrigger Plugin Basics
What you'll learn: How to trigger GSAP animations based on scroll position using the ScrollTrigger plugin.
What Is ScrollTrigger?
ScrollTrigger is GSAP's powerful plugin that lets you control animations based on where the user has scrolled on the page. Instead of animations playing immediately when the page loads, they can start when specific elements come into view, creating engaging scroll-driven experiences.
Think of it like motion-activated lights in a hallway—the lights (animations) only turn on when you (the scroll position) reach a certain point.
Basic Setup
After installing GSAP, you'll need to register the ScrollTrigger plugin:
gsap.registerPlugin(ScrollTrigger);
Creating a Scroll-Triggered Animation
Add a scrollTrigger object to any GSAP animation:
gsap.to(".box", {
x: 500,
duration: 2,
scrollTrigger: ".box" // Triggers when .box enters viewport
});
This animation starts when the .box element scrolls into view.
More Control with Configuration
You can pass an object for detailed control:
gsap.to(".box", {
x: 500,
duration: 2,
scrollTrigger: {
trigger: ".box", // Element that triggers the animation
start: "top center", // When top of .box hits center of viewport
end: "bottom center", // When animation should complete
scrub: true // Link animation progress to scroll position
}
});
The scrub property creates a smooth, scroll-linked animation where dragging the scrollbar moves the animation forward or backward—like scrubbing through a video timeline.
Key Takeaway: ScrollTrigger connects GSAP animations to scroll position, letting you create dynamic, viewport-aware experiences by defining when and how animations play as users scroll through your content.