Sub topic 9
Named timelines with scroll-timeline and view-timeline
This chapter assumes you know @keyframes, the animation properties, and animation-timeline with scroll() and view(). It also assumes you know that an element becomes a scroll container through overflow.
Why name a timeline
The functions scroll() and view() create an anonymous timeline for one element. scroll() looks up to an ancestor scroller. view() tracks the element it is set on. That is enough when the animated element is also the thing being tracked, or when it sits inside the scroller.
Sometimes you want something else. A child might animate based on its parent card's visibility. Several elements might follow one scroller. A named timeline handles these cases. You define the timeline on one element, give it a dashed-ident name that starts with --, and refer to that name from animation-timeline.
| Property | Defines | Value syntax |
|---|---|---|
| `scroll-timeline` | A scroll progress timeline from this element's scroll position | `<name> <axis>` |
| `view-timeline` | A view progress timeline from this element's visibility in its scroller | `<name> <axis>` |
Both shorthands accept the axis keywords block, inline, x, and y. If you leave the axis out, it defaults to block.
A named scroll timeline
The scroller below defines --list. A sticky bar inside it uses that timeline. The bar is a descendant of the scroller, so the name is visible to it with no extra setup.
.scroller {
height: 300px;
overflow-y: auto;
scroll-timeline: --list block;
}
@keyframes fill {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.scroller .bar {
position: sticky;
top: 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: fill linear both;
animation-timeline: --list;
}Declare animation-timeline after the animation shorthand. The shorthand resets it to its initial value.
A named view timeline
view-timeline names a timeline that tracks an element's visibility. The useful part is that other elements can use it. In this example the card defines the timeline, and its image animates against the card's progress.
@keyframes drift {
from { transform: translateY(-30px); }
to { transform: translateY(30px); }
}
.card {
overflow: hidden;
view-timeline: --card block;
}
.card img {
animation: drift linear both;
animation-timeline: --card;
animation-range: cover;
}The image moves a little as the card crosses the scrollport. The image's own box moves too while it animates, so tracking the stable parent gives smoother, more predictable progress than view() on the image would.
You can combine animation-range with a named view timeline as usual. The range names, such as entry, exit, and cover, refer to the element that defines the timeline, not the element being animated.
Scope rules and common errors
A named timeline is visible to the element that defines it and to that element's descendants. An element outside that subtree cannot see the name, and the animation silently has no timeline. The next chapter covers timeline-scope, which widens that visibility.
Check these when a named timeline does nothing:
- The name must match exactly, including the
--prefix and the case. - The defining element must be a real scroll container for
scroll-timeline. Without overflow, there is nothing to scroll. - If two ancestors define the same name, the nearest one wins.
animation-timelinemust come after theanimationshorthand.
Exercise
A .panel element has view-timeline: --panel;. A sibling .label, which is not inside .panel, sets animation-timeline: --panel;. Why does the label not animate, and what fixes it?
Show answer
A named timeline is visible only to the defining element and its descendants, and .label is outside that subtree. Either move .label inside .panel, or declare timeline-scope: --panel; on a common ancestor of both elements.