Sub topic 2
Time-driven versus scroll-driven animation
This chapter assumes you know how @keyframes and the animation-* properties work, how scroll containers and overflow behave, and how to animate transform and opacity. It explains the one idea the rest of the tutorial builds on: an animation needs a timeline, and that timeline does not have to be a clock.
What an animation actually needs
A CSS animation has two separate parts.
- The keyframes describe what changes. For example, a bar grows from
scaleX(0)toscaleX(1). - The timeline decides when each part of that change is shown. It converts "how far along are we?" into a progress value from 0% to 100%.
Most developers never think about the second part, because every animation they have written used the same timeline: the passage of time. The browser's default is the document timeline, which starts when the page loads and advances with the clock. When you write animation: grow 2s linear, you ask the browser to map 0 to 2 seconds of document time onto 0% to 100% of the keyframes.
Scroll-driven animations replace the clock with a different source of progress: a scroll position. The keyframes stay the same. Only the input that moves the animation from 0% to 100% changes.
Time-driven animation
A time-driven animation starts at a moment, usually when the element is styled or a class is added. Progress then increases by itself until the duration is over.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.bar {
transform-origin: left;
animation: grow 2s linear both;
}This animation has these properties:
- Progress depends on elapsed time. After 1 second the bar is at 50%, whatever the user does.
- It moves in one direction. Time only goes forward, so the animation runs and finishes. You can set
animation-iteration-countandanimation-directionto repeat or alternate, but the progress is still driven by the clock. - Duration matters.
2sis a required part of the design. It sets the speed. - The user cannot scrub it. Nothing the user does moves the animation backward or forward. You can pause it with
animation-play-state, but you cannot let the user drag it to a position. - Delay and iteration count matter.
animation-delayshifts the start on the clock, andanimation-iteration-countdecides how many times it repeats.
Scroll-driven animation
A scroll-driven animation has no start moment and no elapsed time. Its progress is a function of where something is scrolled. At the top of the scroller, progress is 0%. At the bottom, it is 100%. If the user scrolls back up, the animation runs backward.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.bar {
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}The @keyframes rule is identical to the one above. Two things changed in the .bar rule:
- The
2sduration is gone. - A new property,
animation-timeline, points the animation at a scroll position instead of the clock.
The next chapters explain animation-timeline and scroll() in detail. For now, read it as "use the root scroller's block-axis scroll position as the timeline."
This animation has these properties:
- Progress depends on position. Stop scrolling halfway and the bar stays at 50%, for as long as you like.
- It is bidirectional. Scrolling up reverses the animation without any extra code.
- The user scrubs it. The scroll thumb, the mouse wheel, the keyboard, a touch gesture, and anchor links all move the animation.
- Duration does not set the speed. The scroll distance does. A longer scrollable area makes the same animation play more slowly per pixel.
The two models side by side
| Aspect | Time-driven | Scroll-driven |
|---|---|---|
| Progress source | Elapsed time on the document timeline | Scroll position of a scroller, or an element's visibility in a scroller |
| Moves without user input | Yes | No |
| Direction | Forward only (unless you set `animation-direction`) | Follows the scroll direction |
| What sets the speed | `animation-duration` | The scrollable distance |
| `animation-timeline` value | `auto` (the default) | `scroll()`, `view()`, or a named timeline |
| Typical use | Transitions, loading indicators, attention cues | Progress bars, reveal effects, parallax |
How the pieces connect
The diagram shows that both kinds of animation share the same keyframes and differ only in what feeds the timeline.
This explains why almost everything you already know carries over. Keyframe percentages, animation-fill-mode, animation-direction and animation-timing-function all still work. They operate on progress, and progress can come from either source.
Two kinds of scroll-driven timeline
The CSS specification defines two types of progress timeline. Later chapters cover each in full, so only the idea is introduced here.
- A scroll progress timeline follows how far a scroll container has been scrolled. Progress is 0% at the start of the scroll range and 100% at the end. A reading progress bar is the standard example.
- A view progress timeline follows how much a particular element has moved through the visible area of its scroller. Progress is 0% when the element begins to enter and 100% when it has fully left. A card that fades in as it appears is the standard example.
The first is about the scroller. The second is about a subject element inside the scroller.
Where the animation runs
A scroll-driven animation is tied to a scroller, which is an element with scrollable overflow, or the viewport. If nothing can scroll, the timeline is inactive. An inactive timeline produces no progress, so the animation does not play.
Two practical results follow:
- If the scroll container has no overflow, there is no distance to scroll. The animation stays at its initial state, so check that the content really overflows.
- Resizing the window, loading images, or changing content changes the scroll range. The animation adjusts to the new range, because progress is recalculated from the current geometry.
Duration and the shorthand
In a time-driven animation, animation-duration is the speed control. In a scroll-driven animation the length of the timeline is the scroll range, so the duration has no say. Its initial value is auto, which for a scroll-driven animation means "fill the whole timeline." For this reason the scroll example above leaves the duration out.
There is one trap with the animation shorthand. The shorthand resets every animation-* longhand it covers, and that includes animation-timeline. If you write animation-timeline first and then the animation shorthand, the shorthand sets the timeline back to auto and the animation silently becomes time-driven again.
/* Wrong: the shorthand comes last and resets the timeline. */
.bar {
animation-timeline: scroll(root block);
animation: grow linear both;
}
/* Right: the longhand comes after the shorthand. */
.bar {
animation: grow linear both;
animation-timeline: scroll(root block);
}Why use scroll position as a timeline
Before scroll timelines, the usual way to link an effect to scrolling was JavaScript: a scroll event listener read scrollTop, computed a ratio, and wrote a style or called a function. That approach has well-known problems:
- The code runs on the main thread, so it competes with layout, script and other work.
- Scroll events can arrive out of step with painting, which can make the effect lag behind the scroll.
- You have to write the ratio calculation, clamping and cleanup yourself.
A declarative scroll timeline moves that work into the browser's animation system. The browser knows the scroll position and the keyframes, so it can update compositor-friendly properties without running your script on every scroll event. Chapter on performance returns to which properties qualify.
Choosing between the two models
Ask what should cause the motion.
- If something should happen because an event occurred, such as a click, a page load, or a state change, use a time-driven animation or a transition. The user did not control the pace.
- If the position of the animation should mirror a scroll position, use a scroll-driven animation. The user controls the pace.
- If an element should play once when it becomes visible and then continue on its own, a scroll-driven animation is the wrong tool. A view timeline is continuous, so it ties every frame to the element's position. For a play-once effect, trigger a normal animation with
IntersectionObserver.
Common misunderstandings
- "Scroll-driven animations play while you scroll." They do not play at all. Their state is computed from the scroll position at each moment. Stopping the scroll freezes the state, rather than pausing a running animation.
- "A longer duration makes it slower." For a scroll timeline, the scroll distance sets the pace.
- "It needs new keyframes." It does not. Existing
@keyframesrules work as they are. - "
animation-fill-modeis optional." Without a fill mode, the element shows its normal styles outside the active part of the timeline. Later chapters discuss when to useboth. For now, note that the examples use it deliberately.
Exercise
A page has .bar { animation: grow 3s linear both; animation-timeline: scroll(root block); }. A teammate says the 3s makes the bar fill in three seconds once the user starts to scroll. Is that right? What actually controls how fast the bar fills?
Show answer
No. With a scroll timeline, progress comes from the scroll position, not from elapsed time, so the bar is at 50% when the page is scrolled halfway and stays there while the user does not scroll. The scroll distance sets how fast the bar fills per pixel. Time-valued durations are not what sets the speed, so the 3s should be removed and the duration left as auto.
Exercise
The following rule is meant to make .bar scroll-driven, but it still runs on a 2-second clock. Find the bug and fix it.
.bar {
animation-timeline: scroll(root block);
animation: grow 2s linear both;
}
Show answer
The animation shorthand comes after animation-timeline and resets it to auto, so the animation uses the document timeline. Put animation-timeline after the shorthand and drop the duration: animation: grow linear both; animation-timeline: scroll(root block);.
Exercise
For each effect, say whether a time-driven or a scroll-driven animation fits better, and why. (a) A spinner shown while data loads. (b) A bar that shows how much of an article has been read. (c) A toast message that slides in when a form is submitted.
Show answer
(a) Time-driven. The spinner must move without any scrolling. (b) Scroll-driven. The bar's state is a direct function of scroll position and should reverse when the user scrolls up. (c) Time-driven. The cause is a form submit event, and the toast should play at its own pace.