javascript
31 lines · 9 steps
Building a scroll-driven reading progress bar
A throttled scroll listener measures how far you've read an article and fills a progress bar to match.
Explained by
highlit
1const progressBar = document.getElementById('reading-progress');
2const article = document.querySelector('article');
3
4function updateProgress() {
5 const { top, height } = article.getBoundingClientRect();
6 const viewport = window.innerHeight;
7 const scrolled = Math.min(Math.max(-top, 0), height - viewport);
8 const total = Math.max(height - viewport, 1);
9 const percent = (scrolled / total) * 100;
10
11 progressBar.style.width = `${percent}%`;
12 progressBar.setAttribute('aria-valuenow', Math.round(percent));
13}
14
15function throttle(fn, limit) {
16 let queued = false;
17 return function throttled() {
18 if (queued) return;
19 queued = true;
20 requestAnimationFrame(() => {
21 fn();
22 queued = false;
23 });
24 };
25}
26
27const onScroll = throttle(updateProgress, 100);
28
29window.addEventListener('scroll', onScroll, { passive: true });
30window.addEventListener('resize', onScroll, { passive: true });
31updateProgress();
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1getBoundingClientRect gives live scroll offsets relative to the viewport without tracking scroll math yourself.
- 2Throttling with requestAnimationFrame caps expensive work to once per frame, keeping scroll handlers smooth.
- 3Updating aria-valuenow alongside the visual width keeps the progress bar accessible to screen readers.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-scroll-driven-reading-progress-bar-explained-javascript-24dd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.