How to Build a Scroll Progress & Reading Indicator in CSS & JavaScript
A scroll reading indicator tracks a reader's position through long-form content. In modern CSS, it can be created with zero JavaScript using Scroll-Driven Animations: animation: growProgress linear; animation-timeline: scroll(); where scaleX expands from 0 to 1 as the root scroller advances. In JavaScript, compute progress as const progress = (window.scrollY / (document.documentElement.scrollHeight - window.innerHeight)) * 100 and map it to a top bar width or circular SVG stroke-dashoffset.
Step 1: Pure CSS Scroll Progress with animation-timeline: scroll()
Modern CSS allows you to link an animation directly to the scrollbar position without running any JavaScript. Define a fixed bar with transform-origin: left and bind it to the root scroller via animation-timeline: scroll().
/* Pure CSS Top Reading Bar */
.scroll-progress-bar {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 4px;
background: linear-gradient(90deg, var(--accent), var(--accent2));
transform-origin: left;
transform: scaleX(0);
z-index: 1000;
/* Link animation progress to window scroll offset */
animation: growProgressBar linear;
animation-timeline: scroll();
}
@keyframes growProgressBar {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
Step 2: JavaScript Math for Universal Cross-Browser Fallbacks
For legacy browsers lacking animation-timeline support, calculate the vertical scroll percentage using high-performance requestAnimationFrame throttling.
const progressBar = document.querySelector('.scroll-progress-bar');
let ticking = false;
function updateScrollProgress() {
const winScroll = window.scrollY || document.documentElement.scrollTop;
const height = document.documentElement.scrollHeight - window.innerHeight;
const scrolled = height > 0 ? (winScroll / height) * 100 : 0;
progressBar.style.transform = `scaleX(${scrolled / 100})`;
ticking = false;
}
window.addEventListener('scroll', () => {
if (!ticking) {
requestAnimationFrame(updateScrollProgress);
ticking = true;
}
}, { passive: true });
Step 3: Circular SVG Radial Progress Meters
Floating circular meters give readers a discrete percentage readout. Use an SVG <circle> and animate stroke-dashoffset proportional to total circumference (2 × π × r).
const circle = document.querySelector('.progress-ring-circle');
const radius = circle.r.baseVal.value;
const circumference = 2 * Math.PI * radius; // e.g. 113.1px for r=18
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = circumference;
function setProgress(percent) {
const offset = circumference - (percent / 100) * circumference;
circle.style.strokeDashoffset = offset;
}
Step 4: Floating Glassmorphic Pills with Back-to-Top Actions
Position a floating pill in the bottom-right corner of long technical articles. Combine the percentage reader indicator with an accessible "Back to Top" trigger that smoothly scrolls the window.
<div class="reading-pill" onclick="window.scrollTo({top: 0, behavior: 'smooth'})" role="button" aria-label="Scroll back to top">
<span class="reading-pill-text">Reading: <strong>68%</strong></span>
<span class="back-to-top-arrow">↑</span>
</div>
<style>
.reading-pill {
position: fixed;
bottom: 24px;
right: 24px;
background: rgba(15, 23, 42, 0.85);
backdrop-filter: blur(12px);
border: 1px solid var(--border);
padding: 8px 16px;
border-radius: 9999px;
cursor: pointer;
z-index: 900;
}
</style>
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why should I prefer CSS animation-timeline: scroll() over window scroll listeners?
CSS scroll-driven animations run directly on the browser's compositor thread, completely off the JavaScript main thread. Even if heavy JS execution locks the main thread, the scroll progress bar continues animating smoothly at 60fps or 120fps.
Why do we divide by (scrollHeight - innerHeight) instead of just scrollHeight?
Because `window.scrollY` can only scroll until the *bottom* of the viewport hits the bottom of the document. When fully scrolled to the bottom, `scrollY` equals `scrollHeight - innerHeight`. Dividing by `scrollHeight` alone would max out around 70%–80% instead of 100%.
How does transform: scaleX() improve rendering performance over width: %?
Animating `width` triggers layout recalculation (reflow) and repaint on every pixel scrolled. Animating `transform: scaleX()` operates solely on GPU composite layers, consuming virtually zero CPU.
How do I track scroll progress through a specific article container instead of the whole page?
Measure the element's bounding box: `const rect = article.getBoundingClientRect()`. Calculate `(window.innerHeight - rect.top) / (rect.height + window.innerHeight)`, or in CSS use `animation-timeline: view()`.
Does a scroll indicator interfere with page accessibility or screen readers?
A decorative scroll bar should have `aria-hidden="true"` and `pointer-events: none` so it does not distract screen readers or block clicks on navigation links beneath it.