Sub topic 2
Time-driven versus scroll-driven animation
This chapter assumes you know CSS @keyframes, the animation shorthand, basic scroll containers (overflow), and transform and opacity. It explains what changes when scroll position, not the clock, drives an animation. Later chapters cover the syntax in detail.
What a timeline is
Every CSS animation has a timeline. The timeline converts "where are we now" into a progress value between 0% and 100%. The browser uses that progress to pick a point between your keyframes.
A normal animation uses the document timeline. This timeline advances with the clock. The animation-duration value tells the browser how many seconds map to 100% progress.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.bar {
animation: grow 2s linear both;
}Two seconds after the animation starts, progress is 100%. The user cannot pause it, reverse it, or scrub through it.
A scroll-driven animation swaps the timeline
A scroll-driven animation uses the same keyframes. Only the timeline changes. Progress now comes from a scroll position, so scrolling forward moves progress forward, and scrolling back moves it back.
.bar {
animation: grow linear both;
animation-timeline: scroll(root block);
}Here, 0% means the page is at the top. 100% means the page is at the bottom. The animation-timeline property selects which timeline drives the animation.
Comparing the two models
| Question | Time-driven | Scroll-driven |
|---|---|---|
| What advances progress? | The clock | Scroll position or element visibility |
| What does `animation-duration` do? | Sets the length in seconds | Has no useful effect; progress is a percentage |
| What happens when the user stops? | Animation keeps running | Animation freezes |
| Can the user reverse it? | No, unless you script it | Yes, by scrolling back |
| Typical use | Transitions, loaders, hover effects | Progress bars, reveal effects, parallax |
Two kinds of scroll-driven timeline
CSS defines two kinds of timeline that follow scrolling.
- A scroll progress timeline tracks how far a scroll container has scrolled. You create it with
scroll(). - A view progress timeline tracks how far an element has moved through its scroll container's visible area (the scrollport). You create it with
view().
This chapter introduces the idea. The next chapters build each kind in detail.
Common mistakes
Setting animation-timeline before the animation shorthand can break the animation. The shorthand resets animation-timeline to its initial value, so declare animation-timeline afterwards.
.bar {
animation-timeline: scroll(root block); /* reset by the next line */
animation: grow linear both;
}Another common mistake is relying on duration for timing. If you write animation: grow 2s linear, browsers that support scroll timelines ignore the duration for progress. The animation still follows scroll. Use linear easing unless you want progress to feel uneven against scroll distance.
Some browsers also need fill-mode. Without both, the element can show its unanimated style outside the active part of the timeline.
Choosing between the models
Use a time-driven animation when the motion should play on its own, such as a spinner or a state change. Use a scroll-driven animation when the motion should answer to the user's position on the page.
Exercise
A progress bar uses animation: grow 3s linear both; animation-timeline: scroll();. The user scrolls to the middle of the page and stops. In a browser that supports scroll timelines, what progress does the animation show after 10 seconds, and why?
Show answer
About 50%. The scroll timeline drives progress, so it reflects the scroll position (the middle of the page). The 3-second duration and elapsed time do not matter, and the animation stays frozen while the user is not scrolling.
Exercise
Why must animation-timeline come after the animation shorthand in a rule?
Show answer
The animation shorthand resets animation-timeline to its initial value. If animation-timeline comes first, the shorthand overwrites it and the animation falls back to the document timeline.