Creating Timelines with gsap.timeline()
You'll learn: How to sequence multiple GSAP animations precisely using gsap.timeline(), controlling when each animation starts and how long they play.
The Problem with Individual Animations
When you use multiple gsap.to() calls in useEffect, they all fire simultaneously. If you want a title to slide in, then a button to fade in, then an image to scale up, you need sequencing. That's where gsap.timeline() shines.
How Timelines Work
Think of a timeline like a movie script where events happen in order. Each animation you add becomes a scene that plays one after another by default:
useEffect(() => {
const tl = gsap.timeline();
tl.to('.title', { x: 0, opacity: 1, duration: 1 })
.to('.button', { opacity: 1, duration: 0.5 })
.to('.image', { scale: 1, duration: 0.8 });
}, []);
This runs the title animation first (1 second), then the button (0.5 seconds), then the image (0.8 seconds).
Position Parameters
You control when an animation starts using position parameters:
const tl = gsap.timeline();
tl.to('.title', { x: 0, duration: 1 })
.to('.button', { opacity: 1, duration: 0.5 }, '-=0.3') // Overlap by 0.3s
.to('.image', { scale: 1 }, '+=0.5') // Wait 0.5s extra
.to('.footer', { y: 0 }, '<'); // Start with previous
'-=0.3': Starts 0.3 seconds before the previous animation ends (overlap)'+=0.5': Adds a 0.5-second gap after the previous animation'<': Starts at the same time as the previous animation
Key Takeaway: gsap.timeline() sequences animations declaratively, while position parameters give you frame-perfect control over timing overlaps and gaps — essential for complex choreographed animations.