Copied!
Guide

How to count numbers up on scroll

The stats band where 0 runs up to 12,480 as it comes into view needs one observer and one small loop. Here is the code, with number formatting and the layout-shift fix most versions miss.

← Blog
Title card: Count-up numbers. Preview of the Counting stats band element from the Enrichr Library.

December 4, 2026

3 min readNo counter library needed

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.

Try it on any site. The free animation library checker shows you which animation libraries actually run on a page (GSAP, Lenis, Framer Motion). Paste a URL, no sign-up for your first lookup.

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?

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?

ElementWhat it does
Counting stats bandA stats band whose numbers count up from zero, rules draw under them and labels fade in one after another.
Scrubbed stats bandA 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

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.