Sub topic 4
Scroll progress: a reading progress bar with scroll()
This chapter assumes you know @keyframes, the animation shorthand, CSS transforms, and how overflow creates a scroll container. It builds on the previous chapter, where animation-timeline replaced the default time-based timeline.
What a scroll progress timeline is
A scroll progress timeline maps how far a scroll container has scrolled to animation progress. At scroll offset 0 the animation is at 0%. At the maximum scroll offset it is at 100%. Scrolling back reverses the animation. No clock is involved.
The scroll() function creates this timeline anonymously and attaches it in one step.
The progress bar
The bar is fixed to the top of the viewport. It starts at zero width and grows 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);
}Add <div class="progress"></div> to a page long enough to scroll, and the bar tracks your position.
Reading the declarations
scroll(root block)has two arguments.rootselects the document's scrolling element.blockselects the block axis, which is vertical in horizontal writing modes.animation: grow linear bothnames the keyframes, sets the easing tolinear, and sets the fill mode toboth. It gives no duration, because scroll position, not time, controls progress.transform-origin: leftmakes the bar grow from the left edge instead of from its center.scaleXanimates a transform, so the browser can usually run it without recalculating layout.
Declaration order matters
The animation shorthand resets every animation sub-property, including animation-timeline, to its initial value. If you write animation-timeline first and the shorthand after it, the shorthand wins and the timeline reverts to the default time-based one. The bar then plays once on load and stays full.
/* Wrong: the shorthand resets animation-timeline */
.progress {
animation-timeline: scroll(root block);
animation: grow linear both;
}Always put animation-timeline after the shorthand.
Why linear and both
- Easing. A non-linear easing makes the bar run ahead of or behind the real scroll position. For a progress indicator the bar should match the scroll position exactly, so use
linear. - Fill mode.
bothkeeps the first keyframe applied before the timeline starts and the last keyframe applied after it ends. Without it, the bar can snap back to its unanimated style at the extremes.
Common errors
| Symptom | Likely cause |
|---|---|
| Bar never moves | The page does not overflow, so there is no scroll range. |
| Bar plays once on load | `animation-timeline` comes before the shorthand, or the browser lacks support. |
| Bar is always full | The shorthand overrode the timeline. |
| Bar grows from the center | `transform-origin` is missing. |
| Bar tracks the wrong container | The scroller argument is wrong. Use `root` for the page. |
Exercise
A progress bar uses animation-timeline: scroll(root block); followed by animation: grow 2s ease-in both;. It does not track scrolling. Name two problems and give the fix.
Show answer
The shorthand comes after animation-timeline and resets it, so the timeline falls back to time. Also, ease-in skews progress away from the scroll position. Move animation-timeline after the shorthand, and use linear easing. The 2s duration is not needed for a scroll timeline.