Sub topic 4
Scroll progress: a reading progress bar with scroll()
This chapter builds a reading progress bar with a scroll progress timeline. It assumes you know @keyframes and the animation-* properties, how overflow creates a scroll container, and how transform and opacity work. The previous chapters covered what a timeline is and how animation-timeline attaches one to an animation. Here you use the first kind of timeline, the scroll progress timeline, from start to finish.
What a scroll progress timeline measures
A scroll progress timeline maps the scroll position of a scroll container onto a progress value from 0% to 100%.
- 0% is the scroll offset of 0, where the container is scrolled to its start.
- 100% is the maximum scroll offset. For a vertical scroller that is
scrollHeight - clientHeight.
The animation then runs along this progress instead of along elapsed time. Scroll halfway and the animation is halfway through its keyframes. Scroll back and the animation plays in reverse. Stop scrolling and the animation stops.
The scroll() function creates this timeline anonymously. You write it directly as the value of animation-timeline, and no other element needs to know about it.
The progress bar
This is the smallest useful example. A fixed bar grows from zero width 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);
}Each declaration has a job:
position: fixedandinset: 0 0 auto 0pin the bar to the top edge of the viewport, stretched across its full width.transform-origin: leftmakesscaleXgrow the bar from its left edge. The default origin is the center, so the bar would grow outward from the middle.animation: grow linear bothnames the keyframes, sets linear easing, and sets the fill mode.animation-timeline: scroll(root block)replaces the default time-based timeline with a scroll progress timeline.
Why scaleX and not width? Animating width forces layout on every scroll frame. Animating transform can run on the compositor, so it stays smooth even when the main thread is busy. The performance chapter returns to this.
Declaration order matters
The animation shorthand resets every animation longhand, including animation-timeline, to its initial value. If you write animation-timeline first and the shorthand second, the shorthand silently wipes it out and the animation runs on time instead.
/* Wrong: the shorthand resets animation-timeline to auto */
.progress {
animation-timeline: scroll(root block);
animation: grow linear both;
}
/* Right: the timeline comes after the shorthand */
.progress {
animation: grow linear both;
animation-timeline: scroll(root block);
}Alternatively, use longhands for everything and the order stops mattering.
Duration is not used
In a time-driven animation, animation-duration controls speed. With a scroll timeline, the scroll distance controls speed, so the duration has no useful role. Leave it out. The initial value is auto, which is what a progress timeline expects. If you need to be explicit, write animation-duration: auto.
Older browser implementations needed auto to be set explicitly when the shorthand included a time value such as 1s. If you copy a time-based animation and add animation-timeline, remove the time value to avoid surprises.
Reading the scroll() arguments
The function takes two optional arguments, in either order:
scroll(<scroller> <axis>)| Argument | Values | Meaning |
|---|---|---|
| scroller | `nearest` (default) | The nearest ancestor that is a scroll container |
| scroller | `root` | The document's viewport scroller |
| scroller | `self` | The element itself, which must be a scroll container |
| axis | `block` (default) | The block axis, which is vertical in horizontal writing modes |
| axis | `inline` | The inline axis, which is horizontal in horizontal writing modes |
| axis | `x` or `y` | The physical horizontal or vertical axis |
So scroll() alone means scroll(nearest block). The next chapter looks at these choices in more detail. For now, two points are enough.
First, the progress bar uses root on purpose. A fixed-position element is not inside the page's content flow, so nearest would not reliably find the document scroller. Naming root removes the ambiguity.
Second, block and y are the same in a normal English page, but they differ in vertical writing modes such as Japanese set top-to-bottom. Logical values follow the text direction. Physical values do not.
A complete page you can run
Save this as progress.html and open it in a Chromium-based browser or another browser that supports scroll-driven animations.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reading progress</title>
<style>
body {
font: 1.1rem/1.6 system-ui, sans-serif;
max-width: 40rem;
margin: 0 auto;
padding: 2rem 1rem 6rem;
}
@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);
}
</style>
</head>
<body>
<div class="progress" aria-hidden="true"></div>
<h1>A long article</h1>
<p>Scroll down to watch the bar fill.</p>
<div style="height: 300vh"></div>
<p>The end.</p>
</body>
</html>The aria-hidden="true" attribute hides the purely visual bar from screen readers. It is decoration, and it carries no information that the page text does not already provide.
Fill mode and easing
Two values in the shorthand are doing real work.
lineareasing keeps the animation's progress equal to the scroll progress. With the defaultease, the bar would run ahead of your scroll position early on and lag behind near the end. For a progress indicator that is a bug, because 50% scrolled should show a half-full bar.bothfill mode applies the first keyframe before the animation starts and the last keyframe after it ends. With a full-range scroll timeline this matters less, but it becomes important onceanimation-rangelimits the active part of the timeline.
Edge cases and failure modes
- The scroller does not overflow. If the page is shorter than the viewport, there is nothing to scroll, and the timeline is inactive. The animation does not progress, and the bar stays at its initial computed state, which is
scaleX(1)orscaleX(0)depending on fill mode and what the inactive timeline reports. Test with a short page so you know what users see. selfon a non-scroller.scroll(self)on an element withoutoverflow: autoorscrollhas no scroll container to measure, so the timeline is inactive.- Content that grows. The timeline is recalculated as layout changes. Lazy-loaded content that extends the page changes the maximum scroll offset, so the bar can move backward without any scrolling. That is correct behavior, because progress is a fraction of the current total.
- Whole-page progress is not article progress.
scroll(root)includes the header, footer, and comments. To track progress through one article only, use a view progress timeline on the article, which a later chapter covers. - Unsupported browsers. A browser that does not recognize
animation-timelineignores it, and the bar runs a time-based animation ofautoduration, which completes instantly and leaves a full bar. The progressive enhancement chapter shows how to guard against this with@supports.
Summary of the model
- The
scroll()function produces an anonymous scroll progress timeline for a chosen scroller and axis. animation-timelinereplaces the time-based clock with that timeline, and it must come after theanimationshorthand.- The scroll offset from start to maximum maps to 0% to 100% of the animation, and duration is ignored.
- Animating
transformkeeps the work off the main thread.
Exercise
A developer writes .bar { animation-timeline: scroll(root); animation: grow linear both; } and the bar fills instantly on page load instead of tracking scroll. What is wrong, and how do you fix it?
Show answer
The animation shorthand comes after animation-timeline and resets it to auto, so the animation uses the default time-based timeline. Move animation-timeline: scroll(root) below the shorthand, or write the animation as longhand properties.
Exercise
You change the bar's easing from linear to ease-in-out. At 50% page scroll, will the bar be exactly half full? Why does this matter for a progress indicator?
Show answer
No. Easing reshapes the mapping from timeline progress to keyframe progress, so at 50% scroll the bar will be at a different fill than 50%, slower near the start and end and faster in the middle. A progress indicator should reflect the true scroll fraction, so use linear.
Exercise
Which scroll() call would you use for a bar inside a scrollable <div class="panel">, where the bar is a child of the panel and should track the panel's own vertical scrolling?
Show answer
scroll(nearest block), or just scroll(), because the panel is the nearest ancestor scroll container of the bar and block is the default axis. If the bar were the panel itself, you would use scroll(self block).