ScrollTrigger Animation in GSAP
- Learn how to trigger animations on scroll using GSAP ScrollTrigger with simple syntax, real UI examples, and best practices.
Introduction
ScrollTrigger is a GSAP plugin that lets you control animations based on the userโs scroll position.
Why use ScrollTrigger?
-
Animate elements when they enter the viewport
-
Create modern scroll-based websites
-
Build storytelling, product pages, and portfolios
Where it is commonly used
-
Landing pages
-
Product showcase websites
-
Portfolio animations
-
Section-based scroll effects
-
Syntax / Basic Concept
ScrollTrigger connects scroll position with GSAP animations.
gsap.to(".box", {
x: 300,
scrollTrigger: ".box"
});
How it works
-
When
.boxenters the viewport -
The animation starts automatically
-
Scroll controls when the animation plays
-
Parameters / Options
Property Meaning Default trigger Element that triggers animation none start When animation starts "top bottom" end When animation ends "bottom top" scrub Sync animation with scroll false pin Fix element during scroll false markers Show start/end markers false
Practical Examples
Fade In on Scroll
gsap.from(".card", {
opacity: 0,
y: 50,
duration: 1,
scrollTrigger: {
trigger: ".card",
start: "top 80%"
}
});
Scroll-Linked Animation (Scrub)
gsap.to(".progress", {
width: "100%",
scrollTrigger: {
trigger: ".section",
scrub: true
}
});
Common Mistakes
โ Forgetting to register ScrollTrigger
-
โ Trigger element not present in DOM
-
โ Using wrong start/end values
-
โ Overusing
scrubcausing performance issues -
โ Not testing on different screen sizes
6. Best Practices
-
Always use
markers: truewhile learning -
Keep animations lightweight
-
Avoid too many pinned sections
-
Use
start: "top 80%"for smooth UX -
Test scrolling on mobile devices
-
Comparison
Feature GSAP ScrollTrigger CSS Scroll Control High Limited Scrub animation Yes No Pin elements Yes No Timeline support Yes No Performance Excellent Medium
Quick Summary
-
ScrollTrigger animates elements based on scroll
-
Works with GSAP timelines
-
Supports pinning, scrubbing, and triggers
-
Ideal for modern interactive websites
-