Scroll-Driven Animations
Scroll-Driven Animations replace scroll event listeners with native timelines: scroll() tracks a container's scroll offset, while view() tracks an element's visibility inside its scrollport. Both run on the Compositor Thread at full display refresh rate.
JS scroll Listener vs animation-timeline: scroll()
Compare main-thread scroll progress bars against native CSS scroll timelines.
window.addEventListener('scroll', () => {
const max = document.body.scrollHeight - innerHeight;
bar.style.width = `${(scrollY / max) * 100}%`;
}, { passive: true });Janks during main-thread work and lags behind async touch scrolling.
@keyframes grow { from { transform: scaleX(0); } to { transform: scaleX(1); } }
.progress-bar {
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}Locked to compositor scroll offset with zero main-thread involvement.
Under the Hood
A JS scroll listener is a backseat driver shouting coordinates over a walkie-talkie; animation-timeline wires the speedometer directly to the axle.
- Timeline Replacement: Instead of wall-clock time, Web Animations API timelines sample progress from
0%to100%based on scroll offset. - Compositor Execution: When animating GPU Compositor Layers properties like
transform: scaleX()oropacity, the Compositor Thread updates pixels without waking the main thread. - Named Ranges:
animation-range: entry 0% cover 40%scopesview()animations to exact entry and exit phases.
@supports (animation-timeline: view()) {
.reveal-card {
animation: fade-slide linear both;
animation-timeline: view();
animation-range: entry 0% entry 80%;
}
}Problem Genealogy
- 2013–2022: Parallax and reading-progress bars relied on throttled
scrollhandlers orIntersectionObserver+ rAF, lagging one frame behind async compositor scrolling. - 2023+: CSS Scroll-Driven Animations introduced
scroll()andview()timelines in Chromium, with Safari 26 following and Firefox implementing.
Production Pitfalls
- Missing
@supportsguard: Settingopacity: 0at0%leaves elements permanently invisible in browsers lackinganimation-timelinesupport. - Animating
widthinstead ofscaleX(): Usingwidthforces Layout on every scroll tick, forfeiting compositor offloading.
- When not to use Scroll-Driven Animations: Do not use them for critical navigation state or binary sticky-header triggers where
IntersectionObserverorposition: stickysuffices.
Connections & Logical Next Steps
Scroll-driven timelines plug into the same Web Animations timing model.
Scroll progress updates on the Compositor Thread without main-thread scroll listeners.
rAF scroll loops sample scrollTop on the main thread, while Scroll-Driven Animations bind timelines to scroll on the compositor.