Scroll-driven animations without JavaScript

CSS has long absorbed patterns developers first built in JavaScript, so it isn't startling that scroll-linked animation now exists in CSS. The scroll-timeline feature has decent browser support, and there is also a JavaScript API for scroll-linked animations when an imperative approach fits better. For anyone avoiding janky main-thread animation in a parallax site, the declarative route is now viable.

One line replaces the script

Chris Coyier's older scroll-linked demo was migrated by replacing the CSS that drove the animation with a single line and deleting the JavaScript entirely.

body, .progress, .cube {
  animation-timeline: scroll();
}

Calling scroll() with no arguments creates an "anonymous scroll progress timeline": the browser attaches the animation to the nearest ancestor that scrolls vertically, given an English writing mode. The function accepts parameters for finer control. One limitation is that the animation can be driven only by scrolling along an element's x or y axis, not both.

Combining several scroll sources

More powerful is the timeline-scope property. Assigned to a container, it allows properties on any chosen ancestor to be animated from any scrollable element sharing that scope. Since CSS Houdini can register inheritable, animation-friendly custom properties, animations on one element can be driven by multiple scrollable regions at once.

An experiment built on this: scrolling a horizontal narrative on a light green card rotates a 3D NES console horizontally, while scrolling a vertical narrative on a dark green card rotates it vertically. The point of interest is the combinatorial explosion of combined rotations from two axes. Earlier CSS hacks could only hide and show a finite set of states; timelines allow a kind of interactivity that pure CSS did not previously offer.

Two custom angle properties are registered:

@property --my-y-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: 0deg;
}

@property --my-x-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: -35deg;
}

The NES model is borrowed from the samples in Julian Garner's CSS 3D modeling app, and the .scene class is updated to base rotation on the new variables:

.scene {
  transform: rotateY(var(--my-y-angle)) rotateX(var(--my-x-angle));
}

The <body> element then receives a timeline-scope carrying two custom names.

body {
  scroll-scope: --myScroller,--myScroller2; 
}

Passing multiple scopes is not officially documented, though it works in Google Chrome and Edge. If it turns out not to be formally supported, it is still worth hoping it becomes part of the standard.

Named timelines are then defined for the two scrollable cards, along with the axis that triggers each animation:

.card:first-child {
  scroll-timeline-axis: x;
  scroll-timeline-name: --myScroller;
}

.card:nth-child(2) {
  scroll-timeline-axis: y;
  scroll-timeline-name: --myScroller2;
}

Finally the animations are attached to the scene:

.scene {
  animation: rotateHorizontal,rotateVertical;
  animation-timeline: --myScroller,--myScroller2;
}

@keyframes rotateHorizontal {
  to {
    --my-y-angle: 360deg;
  }
}

@keyframes rotateVertical {
  to {
    --my-x-angle: 360deg;
  }
}

Because the model inherits both the x and y angles from the document body, scrolling either card rotates it in combinations of horizontal and vertical changes.

Treating scroll areas as sliders

In that experiment the scrollable regions function less as scrollbars than as sliders controlling model properties. That raises the question of whether real range inputs could drive animation timelines; they can, at least in Chrome (see this example), which in turn can control another of Garner's models, an X-wing.

The same technique extends to an arbitrary number of properties. Interesting territory remains unexplored: text fields that progress an animation as they are filled in, or buttons that play and reverse one. Pressing the :active pseudo-class into service alongside animation-play-state can partly achieve the latter, but browsers tend to get confused when that approach animates multiple custom properties. Animation timelines, by contrast, were built for this and behave as expected.

Others have noticed the same potential: a Doom clone has already been built by pairing scroll timelines with checkbox hacks. With Chrome's current capabilities, avatar builders driven by scrollbars and range inputs used as game controls are within reach, along with far less predictable and more sophisticated experiences than existed before scroll timelines.