- Docs
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
- The HTML holds the real value, so it is right without JavaScript, for search engines and for screen readers.
- When the script runs, each number shows its start value (
0by default), formatted for its language. - 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.
- With
prefers-reduced-motion: reduce, the rendered value never changes.
Pieces
| Job | How |
|---|---|
| Final value | data-counter="12480", or read from the text when the attribute is empty |
| Start value | data-counter-from (count down by starting higher) |
| Timing | data-counter-duration, data-counter-delay, data-counter-easing |
| Decimals | Taken from the target; data-counter-decimals to change |
| Language | The closest lang attribute, or data-counter-locale |
| Currencies, compact | The format option (Intl.NumberFormat options) or a formatter |
| Count again | data-counter-repeat |
| Any number, no DOM | animateNumber() |
Choose your next step
Installation
Package managers, the CDN and the first counter.
ExamplesLive previews with the complete, copyable source of each one.
API referenceSignatures and behavior of counter, autoCounter, animateNumber, parseNumber and window.Counter.
Attributes, options and states.
Number formattingLocales, currencies, percentages and layout.
Accessibility and motionReduced motion, screen readers and what your page provides.
Framework integrationsReact, Vue, Svelte, Astro and page swaps.