December 4, 2026
To count a number up when it scrolls into view, watch the element with IntersectionObserver, and when it appears
run a short requestAnimationFrame loop that writes an eased share of the final value into the element. The whole
thing is about twenty lines of plain JavaScript.
What is the code?
<span class="count" data-suffix="+">12480</span>
const fmt = new Intl.NumberFormat();
const calm = matchMedia("(prefers-reduced-motion: reduce)").matches;
function countUp(el, duration = 1600) {
const target = parseFloat(el.textContent.replace(/[^\d.]/g, ""));
const suffix = el.dataset.suffix || "";
el.setAttribute("aria-label", fmt.format(target) + suffix);
if (calm) { el.textContent = fmt.format(target) + suffix; return; }
const start = performance.now();
(function tick(now) {
const t = Math.min(1, (now - start) / duration);
const eased = 1 - Math.pow(1 - t, 3); // ease-out cubic
el.textContent = fmt.format(Math.round(target * eased)) + suffix;
if (t < 1) requestAnimationFrame(tick);
})(start);
}
const io = new IntersectionObserver((entries) => {
for (const e of entries) {
if (e.isIntersecting) { countUp(e.target); io.unobserve(e.target); }
}
}, { threshold: 0.6 });
document.querySelectorAll(".count").forEach((el) => io.observe(el));
.count { font-variant-numeric: tabular-nums; }
Why ease out?
A counter that runs at one speed and stops dead looks mechanical. Ease-out starts fast and slows as it nears the target, so
the eye can catch the final figure. The line 1 - Math.pow(1 - t, 3) is that curve.
How do I stop the layout from shifting?
- Tabular numbers. In most fonts a "1" is narrower than an "8".
tabular-numsgives every digit the same width, so the number does not shake. - Reserve the final width. "0" is much narrower than "12,480". Set a
min-widthinchunits that fits the final value, or the text beside it will slide as the number grows.
How do I handle decimals and currencies?
Pass options to Intl.NumberFormat, for example
{ minimumFractionDigits: 1, maximumFractionDigits: 1 } for "4.8", and skip the Math.round. For money,
use { style: "currency", currency: "EUR" }. The formatter also puts the thousands separator in the right place for
the visitor's language.
Can the number follow the scroll instead?
Yes. Instead of a timed loop, work out the element's progress through the window and use that as the eased value. The number then runs backwards when the visitor scrolls up. The Scrubbed stats band below works that way.
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Counting stats band | A stats band whose numbers count up from zero, rules draw under them and labels fade in one after another. |
| Scrubbed stats band | A framed band that grows to full width while its numbers count up, tied to scroll position and reversible. |
Both are free in the Enrichr Library, with live controls, copyable code and a pen on CodePen.
Where to go next
- Scroll animations without a library.
- How to make a text scramble effect.
- What to do with animations when a visitor asks for reduced motion.
FAQ
How do I animate a number counting up in JavaScript?
Read the target from the element, then on each animation frame work out how much time has passed, ease that progress, and write target * progress into the element until progress reaches 1.
How do I start the counter only when it is visible?
Watch the element with IntersectionObserver and start the count the first time it reports the element as intersecting. Then stop observing so it runs once.
Why should I not use setInterval for a counter?
A fixed step makes a large number take far longer than a small one, and timers drift and keep running in background tabs. A time-based loop on requestAnimationFrame always ends on time.
Will screen readers read every number as it changes?
Not if the element is not a live region. Give the element an aria-label holding the final value so the final figure is what gets announced.