Full answer
Normally a CSS animation moves forward with time. A scroll-driven animation moves with scroll position instead. When the user scrolls, the animation moves forward, and when they scroll back, it moves back. This replaces scroll event handlers for many effects.
There are two kinds of timeline.
- A scroll progress timeline follows how far a scroll container has scrolled.
animation-timeline: scroll()uses the nearest scroller, andscroll(root)uses the page. This suits a progress bar. - A view progress timeline follows how much an element has moved through its scroll container's view.
animation-timeline: view()suits effects that run as an element enters or leaves the screen.
For a progress bar, define keyframes from scaleX(0) to scaleX(1), set animation-timing-function: linear, and attach the timeline. With a timeline, animation-duration is not used for the speed, so set it to auto or leave it. The animation-range property can limit which part of the scroll drives it.
Using transform keeps the animation cheap. The scroll-linked animation can run off the main thread in browsers that support it.
It is a newer feature, so check the support table. Plan the fallback:
- Wrap the effect in
@supports (animation-timeline: scroll()). - Outside that, the bar can stay hidden, or a small script can set the scale from
scrollYon scroll with a passive listener. The page content must not depend on the effect. - Respect reduced motion. A progress bar is not strongly moving, but effects that fly elements around should be turned off in
prefers-reduced-motion: reduce.
At the top of the page the bar has no width. Halfway down, it is half as wide as the window. At the bottom, it is full. In a browser without the feature, the bar stays at zero width, and the page still works.
The fallback here is only a hidden bar. A real page could add a scroll listener for those browsers.
<style>
body { margin: 0; }
#bar { position: fixed; top: 0; left: 0; right: 0; height: 4px; background: #0a58ca; transform-origin: 0 50%; transform: scaleX(0); }
@supports (animation-timeline: scroll()) {
#bar { animation: grow linear both; animation-timeline: scroll(root); }
@keyframes grow { from { transform: scaleX(0); } to { transform: scaleX(1); } }
}
.page { height: 2000px; }
</style>
<div id="bar"></div>
<div class="page">Long content</div>Checked in Chrome 156 only, where scroll-driven animations are available. The bar width was read at the top, the middle and the bottom of the page. The fallback for other browsers was not run. Check the support table. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Scroll-driven animations follow scroll position, not time.
- scroll() tracks a scroller; view() tracks an element in view.
- Animate transform and opacity.
- Wrap in @supports and keep a fallback.
- Respect reduced motion.
Common mistakes
- Using scroll event handlers for effects that CSS can drive.
- Animating layout properties with a scroll timeline.
- Making content depend on the effect, so it fails where the feature is missing.
Follow-ups
- What is the difference between scroll() and view()?
- Why use transform for the bar?
Tips
- Name the two timelines: scroll() and view().
- Say that the effect must be a progressive enhancement behind @supports.