Sub topic 3
Attaching a timeline with animation-timeline
This chapter assumes you can write @keyframes and use the animation-* properties. It also assumes you know what a scroll container is, and that you have seen the difference between time-driven and scroll-driven animation. You will learn how one property, animation-timeline, switches an ordinary CSS animation from the clock to another source of progress.
What the property does
Every CSS animation has a timeline. The timeline is the thing that reports how far along the animation is. By default that is the document timeline, which advances with elapsed time. The animation-timeline property replaces that source with a different one.
Your @keyframes rule does not change. The animation-name, animation-timing-function and animation-fill-mode properties keep their meaning. Only the answer to "how far along are we?" changes.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}The animation starts at scaleX(0) when the page is at the top. It ends at scaleX(1) when the page is scrolled to the bottom. Scrolling back up plays it in reverse. Nothing in the rule mentions seconds.
The accepted values
The property takes a comma-separated list. Each item is one of the following.
| Value | Meaning |
|---|---|
| `auto` | The default. Use the document timeline, so the animation runs on the clock. |
| `none` | No timeline. The animation does not run. |
| `scroll()` | An anonymous scroll progress timeline. Progress follows how far a scroll container has scrolled. |
| `view()` | An anonymous view progress timeline. Progress follows how far the element has moved through its scrollport. |
| `<dashed-ident>` | The name of a timeline defined elsewhere, such as `--list`. |
The next chapters cover scroll() and view() in detail, and later chapters cover named timelines. Here the focus is on how the property itself behaves.
Why the shorthand resets it
The animation shorthand resets animation-timeline to auto. This is the most common error with this property. If you write the longhand first and the shorthand afterward, the shorthand wins and the animation silently goes back to running on the clock.
/* Wrong: the shorthand resets animation-timeline to auto */
.progress {
animation-timeline: scroll(root block);
animation: grow linear both;
}
/* Right: declare the timeline after the shorthand */
.progress {
animation: grow linear both;
animation-timeline: scroll(root block);
}The same rule applies across rules. If a base rule sets animation-timeline and a later rule with equal specificity uses the animation shorthand, the timeline is lost. You see the symptom as an animation that plays once on page load, in your fallback duration, instead of following scroll.
The shorthand also resets animation-range, which a later chapter covers. Place both longhands after the shorthand.
Duration is not the pace
A time-driven animation needs animation-duration to know how long to run. A scroll-driven animation does not. Its length is the length of the timeline, so progress goes from 0% to 100% as the scroll position or visibility goes from start to end.
In the example above, the shorthand animation: grow linear both has no duration at all. The initial value, auto, is what scroll-driven animations expect. If you set a time such as 2s, it does not make the bar grow in two seconds. Scroll distance still controls the pace, so leave the duration out.
Delay is also not a useful control here. To start or stop the animation partway along the timeline, use animation-range. A later chapter covers it.
Easing applies to the progress, not the clock
animation-timing-function still works, but it now reshapes the relationship between scroll progress and animation progress. With linear, a third of the way through the timeline gives a third of the way through the keyframes. With ease-in-out, the animation lags at the start and end of the scroll, and moves faster in the middle.
For effects such as a progress bar, which should match the scroll position exactly, use linear. The default timing function is ease, so omitting it gives a bar that does not match the scrollbar. This is a common cause of "the bar is ahead of my scroll" bugs.
The animation-fill-mode property keeps its normal meaning. With both, the first keyframe applies before the active range and the last keyframe applies after it. Without a fill mode, the element jumps back to its normal style outside the range, which is often visible with view().
Several animations, several timelines
animation-timeline is a list property, so it lines up item by item with animation-name. This lets one element run a clock animation and a scroll animation together.
@keyframes spin {
to { rotate: 360deg; }
}
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
.badge {
animation-name: spin, fade-in;
animation-duration: 4s, auto;
animation-iteration-count: infinite, 1;
animation-timing-function: linear, linear;
animation-fill-mode: none, both;
animation-timeline: auto, view();
}The spin animation uses auto, so it runs for four seconds per turn, forever. The fade-in animation uses view(), so it follows how far the badge has moved through the viewport.
If the list is shorter than the list of names, it repeats from the start. A single animation-timeline: scroll() therefore applies to every animation on the element, which is easy to forget when you add a second animation later. If the list is longer, the extra items are ignored.
Using none as an override
The none value is useful for turning the animation off without removing the keyframes. A common case is a user preference or a variant of a component.
.card {
animation: reveal linear both;
animation-timeline: view();
}
.card.static {
animation-timeline: none;
}An animation with no timeline is inactive and applies no keyframe values. The element shows its normal styles. This differs from auto. Setting auto would start a time-driven animation, and because the duration is auto (which resolves to zero for a time-driven animation), it would jump to the end state. If you want "no animation", write none.
Common mistakes
- Shorthand after the longhand. The shorthand resets the timeline. Always place
animation-timelinelast. - Using
ease. Withoutlinear, scroll progress and animation progress diverge. Set it explicitly. - Expecting duration to matter. A
2sduration does not change the pace. Remove it, or leave itauto. - Missing fill mode. Without
both, the element snaps back to its normal style outside the active range. - Name typos. A dashed name that matches no timeline in scope behaves like an inactive timeline. The animation does nothing and the browser reports no error.
- Animating a property that is not composited. Layout properties such as
widthstill work, but they cost much more per frame. The performance chapter covers this.
Checking your work
You can test a rule quickly with this page. It creates enough content to scroll and fills a bar across the top.
body {
margin: 0;
min-height: 300vh;
background: linear-gradient(white, lightgray);
}
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 6px;
background: crimson;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}Use it with an empty div that has the class progress. Open your browser's developer tools and look at the Animations panel. A scroll-driven animation shows a timeline based on scroll rather than a time axis. If the panel shows a time-based animation instead, the timeline was reset, and the cause is almost always declaration order.
Exercise
A developer writes .box { animation-timeline: view(); animation: reveal 1s ease-out; }. The box fades in once when the page loads, and does not react to scrolling. What are two things wrong, and how do you fix the rule?
Show answer
The shorthand animation comes after animation-timeline and resets it to auto, so the animation runs on the clock. The rule also sets 1s and ease-out, which do not suit a scroll-driven animation. Fix it by writing animation: reveal linear both; first and animation-timeline: view(); after it.
Exercise
An element has animation-name: spin, fade-in; and a single declaration animation-timeline: view();. Which animations follow the view timeline?
Show answer
Both. A list shorter than animation-name repeats from its start, so view() applies to spin and fade-in. To keep spin on the clock, write animation-timeline: auto, view();.