Sub topic 5
Choosing the scroller and the axis
This chapter assumes you know @keyframes, the animation properties, and how overflow creates a scroll container. It builds on the scroll() progress bar from the previous chapter and explains the two arguments you pass to scroll(): which scroller to track and which axis to measure.
The scroll() function takes two arguments
The scroll() function accepts a scroller and an axis, in either order. Both are optional.
animation-timeline: scroll(<scroller> <axis>);If you write scroll() with no arguments, you get the defaults: nearest and block.
Choosing the scroller
The scroller argument decides which scroll container supplies the progress value.
| Value | Meaning |
|---|---|
| `nearest` | The closest ancestor that can scroll. This is the default. |
| `root` | The viewport scroller, which is the document itself. |
| `self` | The element's own scroll container. |
Use root when the animation should follow the page, as in the progress bar. Use nearest when the animated element lives inside a scrolling panel and should follow that panel.
self is useful when an element scrolls and also reacts to its own scrolling, for example a scrollable box whose background shifts as you scroll it.
Choosing the axis
The axis argument decides which scroll direction counts as progress.
| Value | Meaning |
|---|---|
| `block` | The block axis. This is vertical in horizontal writing modes such as English. This is the default. |
| `inline` | The inline axis. This is horizontal in English. |
| `y` | Always vertical, regardless of writing mode. |
| `x` | Always horizontal, regardless of writing mode. |
Prefer block and inline when your layout should adapt to writing mode. Use x and y when the direction is fixed by the design, such as a horizontal carousel.
Example: a horizontal scroller
This example tracks a horizontally scrolling strip. A bar inside the strip's parent fills as the strip scrolls sideways. The bar is a descendant of the scroller's ancestor, but it must be a descendant of the scroller itself for nearest to find it. Here the bar is placed inside the scroller and pinned with position: sticky.
.strip {
display: flex;
gap: 1rem;
overflow-x: auto;
}
.strip .meter {
position: sticky;
left: 0;
align-self: flex-start;
width: 100%;
height: 4px;
background: teal;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(nearest inline);
}
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}The scroll(nearest inline) call picks the .strip element, because it is the closest scrolling ancestor, and measures its horizontal scroll position. scroll(nearest x) would behave the same in English.
Common errors
- The animation never moves. The scroller probably has no overflow on the chosen axis. Check that the content is larger than the box and that
overflowisautoorscroll. - The wrong scroller drives it.
nearestfound an unexpected ancestor, such as a wrapper withoverflow: hidden. Anoverflow: hiddenbox counts as a scroll container even though users cannot scroll it. Userootor a named timeline instead. - The element is not a descendant.
scroll()only finds ancestors (or the element itself). For other elements you need a named timeline, covered in a later chapter.
Exercise
A card sits inside a div with overflow-y: auto. You want the card to animate with the whole page's scroll, not the div's. Which timeline declaration do you write, and why?
Show answer
Use animation-timeline: scroll(root block);. The default nearest would pick the div because it is the closest scrolling ancestor. root selects the viewport scroller, and block measures vertical progress in a horizontal writing mode.