Scroll-driven animations

Advanced · 32 min

How animation-timeline lets scroll position or element visibility drive a normal @keyframes animation, using scroll() and view() timelines and a safe fallback.

Before thisScroll snap and smooth scrollingKeyframe animations

Kevin Powell · 32 min

Scroll-driven animations

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A normal animation's progress is driven by time. With `animation-timeline`, progress is driven by something else: how far a container has scrolled (`scroll()`), or how far an element has travelled through its scroll container's viewport (`view()`).
  • The `@keyframes` are the same ones you already write. The duration no longer means seconds in the usual sense, because the timeline decides how far through the animation you are.
  • `animation-range` limits which part of the timeline maps to the animation, for example `entry 0% entry 100%` to run only while an element slides into view.
  • Put `animation-timeline` after the `animation` shorthand, because the shorthand resets it.
  • Treat it as an enhancement. Gate it with `@supports (animation-timeline: scroll())`, honour `prefers-reduced-motion`, and make sure the content is fully usable when the animation does not run.

Time was always a stand-in

An animation moves from keyframe to keyframe as a clock ticks. For scroll effects, designers do not actually want a clock. They want “when the page is 30% scrolled, the progress bar is 30% full” or “as this card slides into view, it fades in”. For years that meant a scroll event listener in JavaScript on the main thread, measuring and writing styles on every event, which is how pages end up janky.

A scroll-driven animation replaces the clock with a different source of progress. The browser reads the scroll position and sets the animation’s progress directly, usually off the main thread.

The two timelines

scroll() tracks how far a scroll container has been scrolled. A reading-progress bar:

.progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 4px;
  background: dodgerblue;
  transform-origin: left;

  animation: grow linear both;
  animation-timeline: scroll(root block);
}

@keyframes grow {
  from { scale: 0 1; }
  to   { scale: 1 1; }
}

scroll(root block) means “the root scroller, block axis”. At the top of the page the bar is at the from keyframe, at the bottom it is at to. The linear easing matters here, because with the default ease the bar would not match the scroll position.

view() tracks how far an element has moved through its scroll container’s visible area. A reveal effect:

.card {
  animation: fade-up linear both;
  animation-timeline: view();
  animation-range: entry 0% entry 100%;
}

@keyframes fade-up {
  from { opacity: 0; translate: 0 2rem; }
}

The element’s journey has named phases: entry (crossing into view), contain, cover (the whole journey from first touching the viewport to fully leaving), and exit. animation-range: entry 0% entry 100% says the animation should span only the entry phase, so the card is fully visible once it has finished entering and stays that way.

Order matters

The animation shorthand resets every animation longhand, including animation-timeline. So animation-timeline goes after it, as in both examples. Writing them in the other order is the most common reason for “it does nothing”.

The animation-duration is mostly irrelevant for the timeline’s progress. In the examples it is left at its default, which works in current browsers. Some early articles set animation-duration: 1ms to keep older implementations happy.

Named timelines

When the element being animated is not the scroller or the tracked element, name the timeline and share it:

.page { timeline-scope: --gallery; }
.gallery { scroll-timeline: --gallery inline; overflow-x: auto; }
.indicator { animation: slide linear both; animation-timeline: --gallery; }

A timeline name is normally visible only to the element that declares it and its descendants. When the animated element lives elsewhere, like the indicator here, timeline-scope on a common ancestor makes the name reachable. view-timeline-name does the same job for a view() timeline.

Browser support and fallbacks

The data MDN publishes (browser-compat-data 8.1.10, checked today) lists animation-timeline in Chrome and Edge 115, Safari 26, and Firefox 159. If the Firefox figure is still rolling out for your visitors, you need the page to work either way:

.card { opacity: 1; }                       /* default: visible */

@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {
    .card {
      animation: fade-up linear both;
      animation-timeline: view();
      animation-range: entry 0% entry 100%;
    }
  }
}

Defaults first, enhancement inside @supports, motion only for people who have not asked for less. The supports lesson goes through the reasoning. I confirmed in current Chrome that CSS.supports('animation-timeline', 'scroll()') is true, but did not capture frame-by-frame scroll output, so the visual behaviour above is from the specification and documentation.

Good and bad uses

Good: progress bars, header shrinks, a subtle reveal, a parallax backdrop. These are decorative, bound to scroll, and cheap if you animate transform and opacity (see animation performance).

Bad: content that is hidden until animated, anything that gates information on scroll, long chained animations that make the page feel stuck, and large movement for users who prefer reduced motion.

What the video covers

Kevin Powell’s “Incredible scroll-based animations with CSS-only” (December 2023, about 32 minutes) is described as a deep dive into scroll-timeline and the scroll() and view() functions. It looks at a scroll watcher, fading images in as they enter the viewport, moving elements horizontally as you scroll down, and a few more effects, and then covers the polyfill and its limitations. That matches the scroll() and view() sections above. It is from 2023, so its browser support notes are out of date, and its polyfill discussion is not needed for the browsers that now ship the feature. I could not read a transcript. His 2026 short video on the same topic is under further viewing.

Where this fits

This reuses keyframe animations and relies on scroll containers. The final lesson in this group moves from scroll-driven change to page-level change: view transitions.

Check yourself

  1. 01You write `animation: reveal linear both; animation-timeline: view();` for a card, and the card never animates, but if you swap the two lines it works. Why?

    The `animation` shorthand resets `animation-timeline` to its initial value (`auto`, meaning time). Declared after the shorthand, `animation-timeline: view()` is overridden. Declared after it, as in the swapped version, it sticks.

  2. 02What is the difference between `scroll()` and `view()`?

    `scroll()` ties progress to how far a scroll container has been scrolled, from its start to its end, so it suits progress bars and parallax. `view()` ties progress to an individual element's position as it crosses its scroll container's visible area, from just entering to fully leaving, so it suits reveal effects.

  3. 03How would you make an image fade in only while it is entering the viewport, and then stay fully visible?

    Use a `view()` timeline with `animation-range: entry 0% entry 100%` and `animation-fill-mode: both`. The animation maps to the stretch in which the element's leading edge crosses into view, and `both` keeps the final keyframe afterwards.

  4. 04Why must a page using scroll-driven animations still work without them?

    Support is newer than most CSS: Chrome and Edge have had it since version 115, Safari from 26, and Firefox's support is more recent. With `@supports` and defaults that show the content in its end state, an unsupported browser gets a normal page rather than invisible elements or a stuck progress bar.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark