Back to Snippets

Top Loading Progress Bar JS

The loading bar big sites show during navigation. A thin blue line races across the top, slows as it nears the end, then completes and fades out when the work is done.

The bar is a fixed element whose width is driven by a single percentage. Calling start sets it to a small value and begins a trickle, a timer that adds a shrinking random amount each tick so the bar always moves but never quite reaches the end on its own. That is what makes an unknown wait feel like progress. Calling done clears the timer, jumps the width to 100 percent, and after the transition finishes fades the bar out and resets it. A box-shadow on the same element gives the leading edge its glow.

Wire start to the beginning of a fetch and done to its completion, or to the navigation events of a single page app, and the bar reflects real requests instead of the demo timers.

0
#javascript
#css
#progress bar
#loading
#page load
#navigation
#ui