Performance

CSS Scroll-Driven Animations vs JS Scroll Listeners

CSS scroll-driven animations tie an animation's progress to scroll position instead of time, so a progress bar or reveal advances only as the visitor scrolls. I use animation-timeline: scroll() and view() behind an @supports check, keep the static state readable, and reserve JS for interactions the CSS model cannot express.

By Saif Al Jahanwebdev-saif.com

What a scroll-driven animation actually is

CSS scroll-driven animations replace the document time clock with a scroll timeline: keyframe progress comes from a scroller's offset or an element's position in the viewport, not from elapsed seconds. The two functions are scroll() for a scroll container and view() for an element moving through the scrollport. A reading indicator can be one @keyframes rule from scaleX(0) to scaleX(1) plus animation-timeline: scroll(root block) — no listener, no requestAnimationFrame loop, no state variable to maintain. The visitor still scrolls normally; only the clock driving the animation changes. Time-based animation-duration stops being the driver because scroll position supplies the progress. I reach for this pattern on progress bars, sticky section states and small reveals, and I always make sure the element remains usable when the animation does not run at all.

  • animation-timeline: scroll(root block) follows the document scroller
  • animation-timeline: view() follows one element through the viewport
  • Scroll offset supplies progress, not animation-duration
  • Declare animation-timeline after the animation shorthand, which resets it

Building a progress bar with scroll()

The simplest useful example is a fixed top bar: position it at the top, set transform-origin: left, attach an animation named grow from scaleX(0) to scaleX(1), then add animation-timeline: scroll(root block). With no arguments, scroll() means the root scroller on the block axis. The function accepts a scroller of root, nearest or self and an axis of block, inline, x or y, so scroll(nearest inline) suits a horizontally scrolling gallery. For an element outside the scroller subtree, declare scroll-timeline-name: --progress and scroll-timeline-axis: block on the scroller, then reference that name with animation-timeline: --progress. When the animated element is not a descendant, put timeline-scope: --progress on their common ancestor. Animate transform or opacity: those can be composited, while width, top and box-shadow keep the browser doing layout and paint work on every frame.

  • scroll() defaults to the root scroller and block axis
  • scroll(nearest inline) handles horizontal scrollers
  • Named timelines use scroll-timeline-name and scroll-timeline-axis
  • timeline-scope shares a named timeline across sibling subtrees
  • Animate transform or opacity, not geometry or box-shadow

view() and animation-range for reveals and sticky states

view() builds a timeline from an element's own passage through the scrollport, which is what card reveals and section highlights need. The named ranges describe where that element is: cover runs from the first edge entering to the last edge leaving, contain covers the period when it is fully inside, and entry, exit, entry-crossing and exit-crossing mark shorter boundaries. animation-range: entry 0% cover 50% starts on entry and finishes halfway through cover, so a card completes its reveal before it reaches the middle of the screen; animation-range: contain 0% contain 100% holds a state while the element stays fully visible. view-timeline-inset adjusts the scrollport edges when a sticky header covers the top. Use animation-range-start and animation-range-end to tune one edge without rewriting the whole range. Keep keyframes limited to transform and opacity, and the browser has far less work to do per frame.

  • cover, contain, entry and exit describe element positions
  • animation-range: entry 0% cover 50% times a reveal precisely
  • contain ranges hold a state while an element is fully visible
  • view-timeline-inset accounts for sticky headers
  • animation-range-start and animation-range-end tune one edge

Where the CPU savings come from

A JavaScript scroll listener does work on every scroll event: read scrollY, read element geometry, write a style, and often schedule a requestAnimationFrame callback that keeps repeating while scrolling. Mixing reads and writes forces synchronous layout, and a handler that runs too long contributes to Interaction to Next Paint. A CSS scroll timeline is registered with the animation engine: the browser maps scroll offset to keyframe progress and updates the animation without running page JavaScript. When the animated properties are transform and opacity, that update can happen on the compositor, leaving the main thread free for input and rendering. There is no listener to remove, no observer to disconnect and no animation frame loop to cancel. I verify the claim in the Chrome Performance panel: record a scroll, check that no script is attributed to the animation, and watch for Layout events during the gesture.

  • No scroll handler or requestAnimationFrame loop running per frame
  • No forced synchronous layout from reading geometry in a handler
  • transform and opacity can be updated on the compositor
  • Check the Performance panel for Script and Layout rows while scrolling
  • Avoid animating width, height, top or box-shadow on a scroll timeline

Browser support in 2026 and how to detect it

Support is no longer experimental. In caniuse data for October 2026, animation-timeline is supported from Chrome and Edge 115, Firefox 159, Safari 26, iOS Safari 26, Opera 101 and Samsung Internet 23, with global usage around 89 percent. That still means roughly one visitor in ten is on a browser that ignores the property, and older Android devices and embedded WebViews turn up regularly in real traffic. Detect with @supports (animation-timeline: scroll()) instead of user-agent sniffing, and put the whole scroll animation inside that block. If you rely on named timelines or animation-range, test the exact syntax you ship rather than assuming one feature implies another. Recheck MDN or caniuse at build time: support data moves, and a page that hard-codes last year's matrix misleads readers.

  • Chrome and Edge 115 and later
  • Firefox 159 and later
  • Safari and iOS Safari 26 and later
  • Opera 101 and Samsung Internet 23 and later
  • Use @supports, never user-agent strings, for the decision

Progressive enhancement that degrades safely

The baseline must be a complete, readable page with the animation absent. Never start a section at opacity: 0 outside the feature query, or an unsupported browser hides that content permanently. Define the static, final appearance first, then inside @supports add the keyframes and animation-timeline. Pair that with @media (prefers-reduced-motion: reduce) to disable the animation, so visitors who ask for less motion get the same readable state. For effects that genuinely matter on older browsers, add a small fallback: an IntersectionObserver for one-shot reveals, or a passive scroll listener that writes a CSS custom property inside a requestAnimationFrame callback, with geometry cached and recalculated on resize. The fallback enhances content; it never gates it. Keep the fallback code in its own file so browsers with native support never have to download or parse it.

  • Ship the final, visible state as the default style
  • Wrap animation rules in @supports (animation-timeline: scroll())
  • Disable motion under prefers-reduced-motion: reduce
  • Fallback: IntersectionObserver or passive listener plus requestAnimationFrame
  • Cache geometry and recalculate on resize in any JS fallback

Wiring it into a WordPress theme

I am Saif Al Jahan, a WordPress and PHP developer in Mymensingh, Bangladesh. In a WordPress build I enqueue the animation CSS from the theme with wp_enqueue_style and a cache-busting version argument, so a change to a keyframe is not masked by a cached stylesheet. Named timelines and timeline-scope survive minification, but page-builder shortcodes can wrap elements in extra markup and break the assumption that a scrolling ancestor exists — inspect the rendered DOM, not the editor preview. I keep animation values in custom properties so a theme option can switch effects off without editing keyframes, and I avoid inline style attributes because they are awkward to override in a child theme. On webdev-saif.com builds I confirm in the Animations panel that the entry shows a Scroll or View timeline rather than the default document timeline, then recheck INP under continuous scroll.

  • wp_enqueue_style with an explicit version for cache busting
  • Check the rendered DOM when page builders add wrappers
  • Expose timing values as CSS custom properties
  • Confirm the timeline type in the Animations panel

When JavaScript or GSAP is still the right tool

CSS scroll timelines are strong at progress indicators, bounded reveals and simple state changes; they are not a general animation runtime. If you need pinning, scrubbing one timeline across several elements with different offsets, snapping to sections, drag interaction, or playback that reacts to application state, a JS library stays clearer. GSAP ScrollTrigger gives explicit start and end values, callbacks and cleanup, which matters in a theme that mounts and unmounts components. The honest trade-off is cost: keep that library on the pages that actually need it and do not load it for a reading bar. A conditional wp_enqueue_script or a dynamic import keeps the rest of the site on CSS alone. Decide per component by asking whether the effect needs a callback or a state change — if it does, CSS timelines are the wrong abstraction.

  • Pinning, snapping and drag need JS
  • Multi-element scrub with callbacks favours GSAP ScrollTrigger
  • Load heavy libraries only on pages that use them
  • Choose by whether the effect needs callbacks, not by habit

Frequently asked questions

What is a CSS scroll-driven animation in simple terms?

It is an animation whose progress comes from scrolling rather than from a stopwatch. As you scroll, the browser moves the animation from its first keyframe to its last. A reading progress bar that fills as the page moves is the classic example: no JavaScript listens for scroll, and the visual state can be reproduced from the scroll position alone.

Do scroll timelines replace JavaScript scroll listeners entirely?

No. They replace listeners used purely to drive visual progress. You still need JavaScript for pinning, snapping, drag behaviour, complex sequences and anything that must react to state or fire callbacks. Many sites use both: CSS for the common progress and reveal effects, and a small script for the one or two interactions CSS cannot express.

Which browsers support animation-timeline in 2026?

According to caniuse data from October 2026, Chrome and Edge support it from version 115, Firefox from 159, Safari and iOS Safari from 26, Opera from 101 and Samsung Internet from 23. Global usage is around 89 percent. Because the remainder is real traffic, use @supports and ship a readable static state rather than assuming every visitor has native support.

Do CSS scroll-driven animations really save CPU?

They remove the per-frame JavaScript work that a scroll listener normally does: geometry reads, style writes and animation frame callbacks. When you animate transform or opacity, the browser can update those values on the compositor instead of the main thread. That reduces the chance of long tasks affecting Interaction to Next Paint. Animating layout properties such as width or top cancels most of the benefit.

How do I stop content from being hidden in unsupported browsers?

Make the final, visible state your default CSS. Put keyframes and animation-timeline only inside @supports (animation-timeline: scroll()), and do not set opacity: 0 outside that block. Add a prefers-reduced-motion: reduce rule that disables the animation too. Unsupported browsers and reduced-motion users then see the same complete, readable page.

Should I use GSAP instead for scroll animations?

Use GSAP when the effect needs explicit start and end control, callbacks, pinning, snapping or coordination across multiple elements. For a progress bar, a reveal or a simple sticky state, CSS scroll timelines are less code and no library download. In WordPress I enqueue GSAP conditionally so only pages with those effects pay for it.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .