Sub topic 10
Sharing a timeline with timeline-scope
This chapter assumes you know scroll-timeline, animation-timeline, and how @keyframes connect to them. It also assumes you can read a simple parent and child DOM structure.
The problem: names only reach descendants
A named timeline is visible only to the element that declares it and that element's descendants. That works when the animated element sits inside the scroller. It fails when the animated element is a sibling or lives in a different branch of the page, such as a sidebar indicator that shows how far a content list has scrolled.
In that case animation-timeline: --list finds no timeline with that name. The animation then behaves as if no timeline was set, and the element stays at its initial state.
What timeline-scope does
timeline-scope is set on an ancestor. It takes one or more dashed names and makes those timelines visible to the ancestor's whole subtree, including elements that are not inside the scroller. A timeline declared by a descendant, such as .scroller, is attached to the name at the ancestor.
<body>
<div class="indicator"></div>
<div class="scroller">
<p>Item 1</p>
<p>Item 2</p>
<p>Item 3</p>
<p>Item 4</p>
<p>Item 5</p>
<p>Item 6</p>
<p>Item 7</p>
<p>Item 8</p>
</div>
</body>body {
timeline-scope: --list;
}
.scroller {
height: 300px;
overflow-y: auto;
scroll-timeline: --list block;
}
@keyframes fill {
from { transform: scaleY(0); }
to { transform: scaleY(1); }
}
.indicator {
transform-origin: top;
animation: fill linear both;
animation-timeline: --list;
}Here is how the pieces fit:
bodydeclares that--listis a timeline name scoped to everything inside it..scrollerdefines the timeline withscroll-timeline: --list block. The first value is the name and the second is the axis..indicatoris outside.scroller, but it can use--listbecause both elements are descendants ofbody.animation-timelinecomes after theanimationshorthand, because the shorthand resets it.
The diagram shows which elements can see the name.
Choosing where to put the scope
Put timeline-scope on the nearest ancestor that contains both the scroller and every element that needs the timeline. A wide scope such as body is easy to write, but it raises the chance of name collisions. If two descendants declare the same name and are both within the scope, the timeline names can conflict. Keep scopes narrow, and give each timeline a distinct name such as --gallery or --chapter-list.
You can list several names, separated by spaces, in one timeline-scope declaration. Browsers that support timeline-scope: all let you hoist every descendant name at once. Check support before relying on it.
.layout {
timeline-scope: --list, --gallery;
}The same mechanism works for view timelines. Declare view-timeline: --hero block on an element, and put timeline-scope: --hero on a shared ancestor. Other elements can then animate as that element passes through the scrollport.
Common errors
- The scope is on the wrong element. It must be an ancestor of both the scroller and the animated element. Setting it on the scroller or the indicator alone does nothing useful.
- The names differ.
--listand--Listare different names, and the name must start with two dashes. - The timeline is declared but never used. Check that the target sets
animation-timelineto the same name. - The shorthand overrides the timeline. Declare
animation-timelineafteranimation.
Exercise
A progress indicator in a sidebar should follow a scrollable .feed in the main column. The sidebar and .feed share the parent .page. Where do you put timeline-scope, and which property on .feed defines the timeline?
Show answer
Put timeline-scope: --feed on .page, the common ancestor. On .feed, define the timeline with scroll-timeline: --feed block. The sidebar indicator then sets animation-timeline: --feed after its animation shorthand.