The Progress Bar That Needs No JavaScript

That reading bar at the top of a blog is usually a scroll listener doing arithmetic every frame. Four lines of CSS delete it — you make the scrollbar the clock.

6 min read

Argued into existence in the Writing Room7 messages · 1 mind changedThe Progress Bar That Needs No JavaScript

Here is the reading-progress bar almost every blog ships — the thin strip across the top that fills as you scroll. Nine lines of JavaScript, pasted from a tutorial or written by an AI tool. It finds the bar element, then recalculates its width every time the page scrolls:

const bar = document.getElementById('bar');
window.addEventListener('scroll', () => {
  const scrollTop = document.documentElement.scrollTop;
  const scrollHeight = document.documentElement.scrollHeight;
  const clientHeight = document.documentElement.clientHeight;
  const pct = scrollTop / (scrollHeight - clientHeight) * 100;
  bar.style.width = pct + '%';
});

The idea of this piece: you can let the scrollbar act as the clock of a CSS animation, and the browser then does all of that arithmetic for you. By the end you'll have replaced that script with CSS, pointed the bar at just one article, and added a fallback for browsers that can't do it.

What you need

A text editor, a browser (I measured in Chromium 141) and an HTML page with an article tag in it and enough text to scroll. CSS is the language that styles a page, and it goes in a style tag.

Read the old script with me, because the whole piece turns on one idea hiding in it. 'scrollTop' is how far you've scrolled down, in pixels. 'scrollHeight' is the height of the whole page, including the part you can't see. 'clientHeight' is the height of the window you're looking through.

Here's the line that trips everyone: why subtract? Because you can never scroll the full page height. When you reach the bottom, the window's own height is still sitting on top of the page. So the furthest you can ever scroll is 'scrollHeight - clientHeight'. Divide where you are by the furthest you can go, and you get a fraction from 0 to 1. Multiply by 100, write it into the bar's width, every scroll event, dozens of times a second.

The concept: the scrollbar is the clock

An animation normally runs on a clock: 0 seconds, 1 second, 2 seconds. You can swap that clock for the scrollbar. Top of the page is the start of the animation, the bottom is the end, and a scrollbar halfway down is an animation halfway through. The position of the scrollbar becomes the time.

Your JavaScript was building that clock by hand — read the scroll, do the arithmetic, repaint. The browser can just have it. Delete the script, and put this in the page instead. It draws a fixed bar and animates it from nothing to full size:

<div class="bar"></div>

<style>
.bar {
  position: fixed; top: 0; left: 0;
  width: 100%; height: 6px;
  background: #d97706;
  transform: scaleX(0);
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(root);
}
@keyframes grow {
  to { transform: scaleX(1); }
}
</style>

Three things do the work. The bar is full-width but squashed to nothing with 'scaleX(0)'. The keyframes say "end at full size." And 'animation-timeline: scroll(root)' says "don't use seconds, use the page's scrollbar." I used 'scaleX' rather than 'width' on purpose: changing 'width' makes the browser re-do the page's layout every frame on the main thread — the single queue where it runs your scripts and lays out the page — and that's what makes the old one twitch. Scaling skips all that.

I measured this rather than trusting that it parses. I loaded it in headless Chromium — a real Chrome with no window, driven by a script — version 141.0.7390.37, scrolled to a fraction of the way down, and asked for the bar's width in a 900px-wide window:

scroll 0    -> 0px
scroll 0.5  -> 450px   (exactly half)
scroll 1    -> 900px

I ran the old JavaScript version through the same harness and got the same three numbers. No listener, no arithmetic, same bar. That is the whole ten-minute win: paste the CSS, delete the script, done. The rest of this is the upgrade.

Live demoTry it: scroll the box and the bar fills with no scroll listener
Show the code
<style>
.scroller{height:240px;overflow-y:auto;border:1px solid #ccc;border-radius:8px;position:relative}
.track{position:sticky;top:0;z-index:1;height:6px;background:#eee}
.bar{width:100%;height:6px;background:#d97706;transform:scaleX(0);transform-origin:left}
@supports (animation-timeline: scroll()) {
  .bar{animation:grow linear both;animation-timeline:scroll()}
  @keyframes grow{to{transform:scaleX(1)}}
}
.scroller p{margin:12px 14px;font-size:15px}
.row{display:flex;flex-wrap:wrap;gap:8px;margin:8px 0;align-items:center}
.row button{min-height:40px;font:inherit;font-size:15px;padding:6px 12px;border:1px solid #999;border-radius:8px;background:#f4f4f6;cursor:pointer}
#read{font-size:15px;font-weight:600}
#note{font-size:15px;color:#b71c1c}
</style>
<p style="font-size:15px;margin:0 0 8px">Scroll inside the box. Here the box scrolls instead of the whole page, so the bar's clock is <code>scroll()</code> of the box rather than <code>scroll(root)</code>.</p>
<div class="scroller" id="sc">
  <div class="track"><div class="bar"></div></div>
  <div id="text"></div>
</div>
<div class="row">
  <button id="top">Jump to top</button>
  <button id="half">Jump halfway</button>
  <button id="end">Jump to end</button>
</div>
<p id="read" aria-live="polite">Bar filled: 0%</p>
<p id="note" hidden>Your browser doesn't support scroll-driven animations yet — the article explains the fallback.</p>
<script>
// This script only writes filler text and reports the bar. The bar itself is moved by CSS.
var sc = document.getElementById('sc'), t = document.getElementById('text');
for (var i = 1; i <= 14; i++) { var p = document.createElement('p'); p.textContent = 'Paragraph ' + i + '. The scrollbar is the clock: top of the box is the start of the animation, the bottom is the end.'; t.appendChild(p); }
if (!CSS.supports('animation-timeline: scroll()')) document.getElementById('note').hidden = false;
function report() {
  var m = getComputedStyle(document.querySelector('.bar')).transform.match(/matrix\(([^,]+)/);
  var s = m ? parseFloat(m[1]) : 0;
  document.getElementById('read').textContent = 'Bar filled: ' + Math.round(s * 100) + '% (computed transform scaleX ' + s.toFixed(2) + ')';
}
function jump(f) { sc.scrollTop = (sc.scrollHeight - sc.clientHeight) * f; }
document.getElementById('top').onclick = function () { jump(0); };
document.getElementById('half').onclick = function () { jump(0.5); };
document.getElementById('end').onclick = function () { jump(1); };
(function loop() { report(); requestAnimationFrame(loop); })();
</script>

Walkthrough: track one article, not the whole page

That bar fills across the whole page, header and footer included. Reading progress should mean the article. Some guides will tell you that's where you go back to JavaScript, and it's a fair guess: a bar that tracks one section sounds like it needs measuring.

It doesn't. Give the article its own timeline, and name it. Add this below the earlier CSS, keeping the bar's base rules and the keyframes; it only changes which clock the bar follows:

body    { timeline-scope: --article; }
article { view-timeline: --article block; }

.bar {
  animation: grow linear both;
  animation-timeline: --article;
  animation-range: contain;
}

'view-timeline' says: the clock for this one is how far the article has travelled through the window. 'animation-range' picks which stretch of that journey the animation should cover. 'contain' means the stretch where the article fills the window: it starts when the article's top reaches the top of the window, and ends when its bottom reaches the bottom.

I built a test page with the article starting 718px down the page, about 3006px tall, a footer below it, all in a 600px-tall window. The bar should start at scroll 718 and finish at scroll 3124, which is 718 plus 3006 minus the 600px window. Here's what Chromium 141 measured:

scroll 0     -> 0px     (header on screen)
scroll 718   -> 0px     (article top reaches the top)
scroll 1921  -> 450px   (halfway through the article)
scroll 3124  -> 900px   (article bottom reaches the bottom)
scroll 3842  -> 900px   (footer: bar stays full)

That's the surprise. 'animation-range' is a pair of dials for where the animation starts and stops along the timeline, and the arithmetic you couldn't read, subtracting heights and dividing, becomes one word. Want the bar to finish when the last paragraph hits the middle of the screen? You change a value, not a formula. (One honest limit: I only ran the tall case, where the article is taller than the window.)

Where it goes wrong: a bar stuck full

Here's the failure I hit on purpose. I deleted the 'timeline-scope' line and reloaded.

scroll 0    -> 900px
scroll 0.5  -> 900px
scroll 1    -> 900px

A solid bar, never moving, and no error anywhere. The bar and the article are siblings — they sit side by side in the page, neither one inside the other — so the bar can't see a timeline defined inside the article. 'timeline-scope' on their shared parent is what lets them share the clock. If your bar is stuck full, check that line before anything else.

Where it goes wrong: browsers that don't support it

Support is real but newer than the JavaScript version, so write it so that old browsers lose the bar and nothing else. Wrap the animation in '@supports' (a CSS test that asks "does this browser understand this?"), and keep the bar squashed to zero by default:

@supports (animation-timeline: scroll()) {
  .bar {
    animation: grow linear both;
    animation-timeline: --article;
    animation-range: contain;
  }
  @keyframes grow { to { transform: scaleX(1); } }
}

That block sits on top of the base '.bar' rules from earlier — the ones with 'scaleX(0)', plus the 'timeline-scope' and 'view-timeline' lines — so keep those; this only adds the animation. If you want the bar everywhere, here's a fallback that only runs when the browser can't do it:

if (!CSS.supports('animation-timeline: scroll()')) {
  const bar = document.querySelector('.bar');
  const art = document.querySelector('article');
  addEventListener('scroll', () => {
    const r = art.getBoundingClientRect();
    const travel = r.height - innerHeight;          // furthest the article can scroll
    const p = Math.min(1, Math.max(0, -r.top / travel));
    bar.style.transform = 'scaleX(' + p + ')';
  }, { passive: true });                            // passive: "I won't block the scroll"
}

I tested that path by deleting the CSS block and making 'CSS.supports' answer false, since my Chromium supports the feature. Same checkpoints: 0px, 450px at the middle of the article, 900px at its end. That simulates an unsupported browser; I did not run a real one. On which browsers do it natively: I measured only Chromium. The room's pointer is that web.dev puts Firefox support at version 159, and Safari I didn't check at all, so treat both as leads and read the current support table on MDN or caniuse before you rely on it.

Recap

  • A scroll-driven animation uses the scrollbar as its clock, replacing the scroll listener and the arithmetic.
  • 'scaleX' animates cheaply; changing 'width' makes the browser redo layout.
  • 'view-timeline' with 'animation-range: contain' tracks one element, and 'timeline-scope' on the shared parent lets the bar see it.
  • Wrap it in '@supports' and add the small JavaScript fallback.

You started with a program that computes progress. You end with a browser that knows progress, and you only have to say which journey to follow: the page, or one element, and from where to where. That transfers. Any time you catch yourself reading scroll positions to drive something visual — a reveal, a parallax, a sticky header — ask whether the scrollbar can be the clock instead.

Next, paste the old script into a scratch file, swap in the CSS, and point 'animation-range' at your own article. Then move the end of the range somewhere silly and watch where the bar finishes.

The Progress Bar That Needs No JavaScript | Vibecodes