Skip to main content

2026-11-27 · 3 min read

A scroll-driven navbar background and back-to-top button in pure CSS

CSSPerformance

Translated from the Chinese original: read it in Chinese

This site has three things that only appear once you have scrolled past a certain point: the translucent background of the home page navbar, the top bar on sub-pages, and the "↑" back-to-top button in the bottom right corner. All three used to work the same way: JavaScript listens for the scroll event and toggles a class once you cross a threshold.

Nothing wrong with that, but it has two small drawbacks. JS has to run its check over and over while you scroll, and the effect snaps on and off instead of appearing gradually as you scroll.

CSS can use the scroll position as the animation timeline

animation-timeline: scroll() makes an animation follow "how far the page has scrolled" instead of time. Combine it with animation-range to say where it starts and where it finishes, and the browser derives the progress from the scroll position with no JavaScript at all.

I moved the navbar's background color, blur and bottom border onto a ::before layer (they used to be painted on the header itself) and let its opacity go from 0 to 1:

.nav-fade::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--nav-bg);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  opacity: 0;
  transition: opacity 0.5s var(--ease-ma); /* used by browsers without support */
}
.nav-fade.is-solid::before { opacity: 1; }  /* class added by the JS fallback */

@supports (animation-timeline: scroll()) {
  .nav-fade::before {
    transition: none;
    animation: nav-fade-in linear both;
    animation-timeline: scroll(root);
    animation-range: 0 60px;               /* while scrolling from 0 to 60px */
  }
}
@keyframes nav-fade-in { from { opacity: 0 } to { opacity: 1 } }

The back-to-top button works the same way. Its range is "the 80px after one full viewport height", and it slides up from below while fading in:

@supports (animation-timeline: scroll()) {
  .back-to-top-fade {
    animation: back-to-top-in linear both;
    animation-timeline: scroll(root);
    animation-range: 100vh calc(100vh + 80px);
  }
}
@keyframes back-to-top-in {
  from { opacity: 0; visibility: hidden; translate: 0 8px; }
  to   { opacity: 1; visibility: visible; translate: 0 0; }
}

animation-fill-mode: both (the both in the animation shorthand above) matters here: before the scroll position reaches the range, the button has to stay in its from state, which means invisible and not clickable.

Browsers without support keep the original JavaScript

The whole block sits inside @supports (animation-timeline: scroll()). Browsers that do not support it never see this CSS and keep using the original JS: add .is-solid past the threshold, or toggle the button's class. In browsers that do support it, the JS is in fact still running. The values it sets are simply overridden by the CSS animation, and both paths end in the same result. So what this approach buys is "the effect follows the scroll position" and "no waiting for JS to load", not "no JS at all".

Testing it with JavaScript turned off

The most direct way to confirm that CSS alone is doing the work is to open a Playwright browser context with javaScriptEnabled: false, scroll to different positions, and read the opacity from getComputedStyle. For the "↑" button in a 700px-tall viewport:

Scroll positionopacityvisibility
0 px0.00hidden
600 px0.00hidden
720 px0.25visible
760 px0.75visible
900 px1.00visible

The navbar's ::before reads 0, 0.33, 0.67 and 1 at scroll positions 0, 20, 40 and 60px. The numbers match the progress computed from animation-range exactly.

A test that had to change

The original E2E test said "after scrolling past 60px, the header's background-color is no longer transparent". With the background moved to ::before, the header itself is always transparent, so that test no longer meant anything. It now reads the opacity of ::before: 0 at the start, and greater than 0.9 after scrolling to 400px. That way, whether the CSS path or the JS fallback is active, the test checks what the user actually sees.

Summary

  • animation-timeline: scroll() plus animation-range: the animation follows the scroll position, with no scroll listener
  • Paint the parts that should fade (background, blur, border) on ::before and animate only its opacity
  • fill-mode: both keeps the element in its starting state before the range is reached
  • Wrap it in @supports so browsers without support fall back to JS, and make the test check what the user sees
  • The most direct verification: turn JavaScript off and read the computed style

Related commits: 860c455, 8358fe8 (GitHub)

Type to search projects and posts too