Sub topic 3
Attaching a timeline with animation-timeline
This chapter assumes you know @keyframes, the animation shorthand, and how scroll containers work. Here you learn the one property that switches an animation from clock-driven to scroll-driven.
What the property does
Every CSS animation has a timeline. The timeline reports how far along the animation is, as a progress value from 0% to 100%. By default the timeline is the document timeline, which advances with the clock. animation-timeline replaces it with a different source of progress.
animation-timeline: auto | none | scroll() | view() | <dashed-ident>;| Value | Progress comes from |
|---|---|
| `auto` | The document timeline (the clock). This is the default. |
| `none` | Nothing. The animation has no timeline and does not run. |
| `scroll()` | How far a scroll container has scrolled. |
| `view()` | How much of an element is visible in its scroller. |
| `--name` | A named timeline defined elsewhere (covered in a later chapter). |
The keyframes do not change. Only the thing that moves the animation forward changes.
A minimal example
This bar grows from zero to full width as the page scrolls.
@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);
}Scroll position 0 maps to the from keyframe. The end of the scroll range maps to to. Scrolling back reverses the animation, because progress follows the scroll position and nothing else.
Duration and delay no longer behave as before
A time-driven animation needs a duration to know how long it takes. A scroll-driven animation does not. Its length is the scroll distance, so the browser stretches the keyframes across the whole timeline. Leave animation-duration out, or leave it at auto.
Time-based animation-delay has no useful meaning here either. To start or stop part-way along the timeline, use animation-range, which a later chapter covers.
animation-iteration-count and animation-direction still apply. Easing and fill mode are also still in play, but they act on progress, not time.
Declaration order matters
The animation shorthand resets every animation longhand, including animation-timeline, to its initial value. If the shorthand comes after the timeline, the timeline is wiped out.
/* Broken: the shorthand resets animation-timeline to auto */
.progress {
animation-timeline: scroll(root block);
animation: grow linear both;
}
/* Works: the timeline is set after the shorthand */
.progress {
animation: grow linear both;
animation-timeline: scroll(root block);
}In the broken version the animation runs on the clock. Because the duration is 0s, it jumps to its end state at once, which looks like a bar that is always full.
Common errors
- Setting a duration and expecting it to matter. It is ignored when a scroll or view timeline is attached.
- Using
nonewhen you meanauto. Withnonethe animation never progresses. Withautoit falls back to time. - Forgetting
lineareasing. The defaulteasebends progress, so the element's motion no longer lines up with the scroll position. The easing chapter explains this. - Using a scroller that does not scroll. If the chosen container has no overflow, progress stays at 0% and nothing visibly happens.
Exercise
A rule sets animation-timeline: view(); and, on the next line, animation: reveal linear both;. The element animates once on page load instead of following scroll. Why, and what is the fix?
Show answer
The animation shorthand comes second, so it resets animation-timeline to auto and the animation runs on the document timeline. Move animation-timeline: view(); below the shorthand.