NOSCRIPT

Page 01: The listener

Retires addEventListener("scroll") and the IntersectionObserver sentinel

For about fifteen years, anything that moved when you scrolled was a function that woke up on every scroll event, read a number and wrote a style.

It worked, at a price. The handler ran on the main thread, the same one that was busy laying out the page, so the effect trailed behind your finger whenever the page had other work to do. Libraries grew up around throttling it. IntersectionObserver later took over the “is this on screen yet” half of the job, and the rest stayed where it was.

The replacement is to stop asking. A CSS animation normally runs against a clock. Since Chrome 115, Safari 26 and Firefox 159 it can run against a scroll position instead. The red bar at the top of this page is one such animation, and the percentage at its end is the same animation printed as a number.

Wasruns on every scroll event
let ticking = false;
addEventListener("scroll", () => {
  if (ticking) return;
  ticking = true;
  requestAnimationFrame(() => {
    const max = document.documentElement
      .scrollHeight - innerHeight;
    bar.style.width =
      (scrollY / max) * 100 + "%";
    ticking = false;
  });
}, { passive: true });
Nowruns nothing
.progress {
  animation: read linear both;
  animation-timeline: scroll(root);
}
@keyframes read {
  from { --pct: 0 }
  to   { --pct: 100 }
}

One caveat, since this page is supposed to be honest. A scroll-driven animation of transform or opacity can be handed to the compositor and stay smooth while the main thread is blocked. The bar animates a custom property so that it can print its own value, and custom properties are computed on the main thread. It costs nothing you would notice here, but it is not the fast path.

A drum geared to the page

The next screen holds twelve lines of code on the faces of a drum. It stays where it is while you scroll and turns by exactly as much as you have moved: 330 degrees from one end of the section to the other. Stop and it stops. Scroll back up and the lines come back the way they went. The first six faces are the old listener. Keep going and the drum carries them out of sight and brings up the CSS that replaced them, which is also the CSS that is turning the drum.

In a browser with scroll-driven animations, and with no request to reduce motion, the twelve lines below sit on the faces of a drum that turns as you scroll. Here they are as a plain listing instead: the old listener, struck out, and then the CSS that replaced it.

  1. // the old way: wake up on every event
  2. addEventListener("scroll", () => {
  3. const t = scrollY / (h - innerHeight);
  4. drum.style.transform =
  5. "rotateX(" + t * 330 + "deg)";
  6. });
  7. /* the new way: say it once */
  8. .drum {
  9. animation: turn linear both;
  10. animation-timeline: --pin;
  11. }
  12. @keyframes turn { to { --turn: 330deg } }

What turned it. The tall section declares view-timeline: --pin. The drum inside it names that timeline, and its keyframes run from 0 to 330 degrees. Each face is placed with rotateX() and pushed out by the drum's radius, which the browser derives from the face height with tan(). The two numbers under the drum are a second animation on the same timeline, read out through a CSS counter.

Section: sticky things

Am I stuck?

A sticky header has always been CSS. Knowing that it is currently stuck, so that it can shrink or grow a shadow, was not. The usual trick was a one-pixel sentinel element placed just above the header, and an IntersectionObserver watching for it to leave the screen. Scroll-state container queries ask the question directly.

The bar above this paragraph turns dark and reports “stuck: yes” for as long as it is pinned to the top of your window. Chrome has had this since version 133. Safari and Firefox have not shipped it.

In this browser the bar above sticks, but cannot tell that it is stuck, and says so. Chrome 133 and later can. Nothing is broken: the bar is as sticky as it ever was.

Wasan extra element to watch
const io = new IntersectionObserver(
  ([entry]) => bar.classList.toggle(
    "stuck", !entry.isIntersecting));
io.observe(sentinel);
Nowa question
.bar {
  position: sticky; top: 0;
  container-type: scroll-state;
}
@container scroll-state(stuck: top) {
  .bar-in { background: var(--ink); }
}

A carousel that builds its own controls

Scroll listeners had smaller jobs too. Five of them are on the cards below, which are a specimen of the fifth.

  • 1 of 5

    Reveal on enter

    Was an IntersectionObserver adding a class. Now a keyframe animation with animation-timeline: view() and a range of entry.

  • 2 of 5

    Parallax

    Was a scroll handler writing translateY at a fraction of the scroll offset. Now two layers on one view timeline with different keyframes.

  • 3 of 5

    Snap to slide

    Was touch handlers and hand-written momentum. Has been scroll-snap-type for years, in every browser, and is what this list is made of.

  • 4 of 5

    Scroll-spy

    Was a table of offsetTop values compared on every scroll. Is now :target-current, in Chrome only so far.

  • 5 of 5

    Dots and arrows

    Was the rest of the carousel library. Is now ::scroll-marker and ::scroll-button(), in Chrome 135 and later.

The arrows and dots are not in the HTML. The stylesheet asks for them and the browser builds them, wires them to the list, disables an arrow at either end and moves the red dot as you swipe. They are real controls: Tab reaches them.

A scroll-snap list. Swipe it, or focus it and use the arrow keys. In Chrome 135 and later the stylesheet also asks the browser for arrows and dots, and gets them. Safari and Firefox have not shipped that part, so here the scrollbar does the job.