MotionLift

Animated number counters and charts built from the figures on the page

By Pulse Labs · Published · Updated · 2 min read

An animated counter rolls a number up to its real value when it scrolls into view, and an animated chart draws its bars the same way. The safe way to build both is to put the true figures in the HTML first and let a few lines of script animate them. Then the page is correct with scripts off, with reduced motion on, and in search results.

The demo below does both from one table. The table is the source of truth: the script reads its figures, draws the bars from them, and rolls the numbers. It animates transform, not width, so nothing on the page moves.

Live demo Counters and bars from one table
Markup · html
<table id="fig">
  <caption>Cash on hand, by quarter</caption>
  <tbody>
    <tr><th scope="row">Q1</th><td data-v="42000">$42,000</td></tr>
    <tr><th scope="row">Q2</th><td data-v="61000">$61,000</td></tr>
    <tr><th scope="row">Q3</th><td data-v="88000">$88,000</td></tr>
    <tr><th scope="row">Q4</th><td data-v="120000">$120,000</td></tr>
  </tbody>
</table>
<p class="foot">Growth: <strong id="g" data-v="186">186%</strong> over the year</p>
Styles · css
table { width: 100%; border-collapse: collapse; }
caption { text-align: left; margin-bottom: 8px; color: #a79fc9; }
th { width: 48px; text-align: left; font-weight: 600; }
td { position: relative; padding: 8px 0 8px 0; font-variant-numeric: tabular-nums; }
td .num { display: inline-block; min-width: 7ch; }
td .bar { display: block; height: 6px; margin-top: 4px; border-radius: 3px; background: #c8f250; transform-origin: 0 50%; transform: scaleX(var(--s, 1)); transition: transform .8s cubic-bezier(.16, 1, .3, 1); }
.foot { margin-top: 18px; font-variant-numeric: tabular-nums; }
@media (prefers-reduced-motion: reduce) { td .bar { transition: none; } }
Script · js
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
const cells = [...document.querySelectorAll("td[data-v]")];
const max = Math.max(...cells.map((c) => +c.dataset.v));
const fmt = (c, n) => (c.dataset.v > 999 ? "$" + Math.round(n).toLocaleString("en-US") : Math.round(n) + "%");
function wrap(c) {
  const text = c.textContent;
  c.innerHTML = '<span class="num"></span><span class="bar"></span>';
  c.querySelector(".num").textContent = text;
  return c;
}
cells.forEach((c) => { wrap(c); c.querySelector(".bar").style.setProperty("--s", reduce ? c.dataset.v / max : 0); });
const roll = (el, to, fmtFn) => {
  if (reduce) return;
  const t0 = performance.now();
  (function tick(t) {
    const p = Math.min(1, (t - t0) / 900), e = 1 - Math.pow(1 - p, 3);
    el.textContent = fmtFn(to * e);
    if (p < 1) requestAnimationFrame(tick); else el.textContent = fmtFn(to);
  })(t0);
};
const g = document.getElementById("g");
const pending = new Set([...cells, g]);
function show(el) {
  pending.delete(el); io.unobserve(el);
  if (el === g) return roll(g, +g.dataset.v, (n) => Math.round(n) + "%");
  el.querySelector(".bar").style.setProperty("--s", el.dataset.v / max);
  roll(el.querySelector(".num"), +el.dataset.v, (n) => fmt(el, n));
}
const io = new IntersectionObserver((entries) => {
  for (const e of entries) if (e.isIntersecting) show(e.target);
  pending.forEach((el) => { if (el.getBoundingClientRect().bottom < 0) show(el); });
}, { threshold: 0.6 });
if (!reduce) pending.forEach((el) => io.observe(el));

Three details do the work. font-variant-numeric: tabular-nums and a min-width keep digits from changing the width as they count. The final text is always set to the exact value, so a rounding error in the roll can never show. And under reduced motion the script exits before changing anything, leaving the figures and the full bars.

Rules for counters and charts

  • Real figures in the HTML. Never generate numbers in script that are not on the page.
  • Roll once, when the number arrives. Counting again on every pass is distracting.
  • Animate transform and opacity, so the page does not shift. Animation and page speed shows the measured difference.
  • Keep the chart readable without animation: label it, and keep a table or text alternative.

If you would rather use a library, CountUp.js has an option to start counting when the element scrolls into view. Whatever you choose, keep the final number in the HTML, so search engines and visitors without JavaScript see it.

Doing it for figures you already have

If your page already has prices, percentages or a table, MotionLift reads them and builds from them. Rolling numbers roll figures into place as they arrive. Moving graphs turn prices and figures side by side into bars, percentages into rings, fill in tables, and make your own charts draw themselves. Nothing is invented: the graphs come from figures already on the page. The Harbor Ledger example, a made-up cash-flow product, shows it, and its Data style puts graphs and numbers first.