Guide

Counter

What @samline/counter does, why the final value stays in the HTML, and how counting, formatting and repeat fit together.

@samline/counter counts numbers up (or down) as they scroll into view. Render the final value, put the plain number in data-counter and call autoCounter() (or load the CDN script).

<p><span data-counter="12480">12,480</span> customers</p>

How it behaves

  1. The HTML holds the real value, so it is right without JavaScript, for search engines and for screen readers.
  2. When the script runs, each number shows its start value (0 by default), formatted for its language.
  3. When half of it is visible, it counts to the final value over two seconds with an ease-out curve, and ends exactly on it.
  4. With prefers-reduced-motion: reduce, the rendered value never changes.

Pieces

JobHow
Final valuedata-counter="12480", or read from the text when the attribute is empty
Start valuedata-counter-from (count down by starting higher)
Timingdata-counter-duration, data-counter-delay, data-counter-easing
DecimalsTaken from the target; data-counter-decimals to change
LanguageThe closest lang attribute, or data-counter-locale
Currencies, compactThe format option (Intl.NumberFormat options) or a formatter
Count againdata-counter-repeat
Any number, no DOManimateNumber()

Choose your next step