GSAP Timelines Basics
You'll learn how to sequence multiple animations together using GSAP timelines for synchronized, coordinated motion.
The Core Idea
So far you've animated individual elements with gsap.to(), gsap.from(), and gsap.fromTo(). But what if you want multiple animations to happen one after another, or overlap in specific ways? That's where timelines come in.
A timeline (gsap.timeline()) is like a container that holds multiple animations and controls them as a single sequence. Think of it like a music playlist: instead of playing songs manually one at a time, you arrange them in order and hit play once.
Creating a Timeline
const tl = gsap.timeline();
tl.to(".box1", { x: 100, duration: 1 })
.to(".box2", { y: 50, duration: 0.5 })
.to(".box3", { rotation: 360, duration: 1 });
This creates three animations that play sequentially: box1 moves right, then box2 moves down, then box3 rotates.
Why Timelines Matter
Without timelines, you'd need to manually calculate delays:
// Clunky approach:
gsap.to(".box1", { x: 100, duration: 1 });
gsap.to(".box2", { y: 50, duration: 0.5, delay: 1 }); // Wait for box1
gsap.to(".box3", { rotation: 360, duration: 1, delay: 1.5 }); // Wait for both
Timelines automatically handle the sequencing math for you. If you change the first animation's duration, everything else adjusts automatically.
Timeline Benefits
- Sequential animations without manual delay calculation
- Control the entire sequence with one play/pause/restart command
- Reusable animation sequences
- Foundation for complex choreography (covered in later lessons)
Key Takeaway: gsap.timeline() lets you chain animations together effortlessly, creating coordinated sequences without manually managing delays or timing.