counter
Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
Open water season 2026
12,480 swimmers
crossed the bay with a club coach since the first of May.
Live preview needs WebGPU or WebGL 2. A swim club's season stat whose digits turn on odometer drums from 0 up to 12,480 and fold back into the text.
This example failed to draw.
Every switch and slider counts it again.
Usage
Put it on a heading, paragraph or span holding one number, with any text before and after it, such as $4.2M, 99.9% or 12,480 swimmers. start() registers counter, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add counter<div class="cn-stat"> <p class="cn-kicker">Open water season 2026</p> <p class="cn-number" data-dio="counter"> 12,480 <span>swimmers</span> </p> <p class="cn-note">crossed the bay with a club coach since the first of May.</p></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.cn-number', 'counter', { style: 'odometer', trigger: 'view', duration: 2, from: 0, stagger: 0.12, delay: 0, threshold: 0.3,})import { start } from '@dioramajs/diorama'import counter from '@dioramajs/text/counter'
const app = start()app.attach('.cn-number', counter({ style: 'odometer', trigger: 'view', duration: 2, from: 0, stagger: 0.12, delay: 0, threshold: 0.3,}))Page transitions
Entering a page the number counts up from its start value; leaving, it counts back down to it. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
stylerebuildsdata-dio-style | 'odometer' | 'slot' | 'flip' | 'odometer' |
|
triggerrebuildsdata-dio-trigger | 'view' | 'scroll' | 'view' |
|
durationdata-dio-duration | number0.3 to 6 s | 2 | Time of the count; with slot and flip, until the last digit lands. |
fromdata-dio-from | number0 to 100000 | 0 | Value the count starts from, at most what the digits of the number can hold. |
staggerdata-dio-stagger | number0 to 0.5 s | 0.12 | Delay between neighboring digits landing, for slot and flip. |
delaydata-dio-delay | number0 to 3 s | 0 | Wait after the trigger, for a row of stats. |
thresholddata-dio-threshold | number0 to 1 | 0.3 | Share of the element height in view, above the bottom tenth, before it counts. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads height, presence, progress, size, width, x and y
. Draws only while something on it moves, then stops. Only while the digits turn and fold back, and with the scroll trigger while scrolling; none at rest. How a frame is decided.
Reduced motion and touch
With reduced motion it is off and the element shows as plain HTML. Off: the number is shown as it is, and the engine does not load for it.
Runs on every device. The same as with a mouse: nothing reads the pointer.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { counter: { reducedMotion: 'static' } } })Cost and quality
- Download
- 12.9 KB min+gzip the first time a page uses it
- GPU
- A strip of 24 quads per digit with ten texture reads per pixel for the motion blur, a quad per letter around the number.
- Memory
- A canvas of the text at twice its CSS size and a small sheet of the digits, kept while the element lives.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | The text and the digits are rastered at 1 device pixel per CSS pixel instead of 2. |
Recipes
A row of stats that count in turn
Give each stat its own element and a growing delay, so the numbers land one after another instead of all at once. Each number can take its own style. Scroll down to this example to see it count.
312
clubs on the platform$4.2M
raised for open water safety99.9%
of sessions booked online
Live preview needs WebGPU or WebGL 2. Three stats of a swim club platform that count up in turn on drums, slot reels and flip cards.
This example failed to draw.
<ul class="cn-row"> <li> <p data-dio="counter">312</p> <span>clubs on the platform</span> </li> <li> <p data-dio="counter" data-dio-style="slot" data-dio-delay="0.25" >$4.2M</p> <span>raised for open water safety</span> </li> <li> <p data-dio="counter" data-dio-style="flip" data-dio-delay="0.5" >99.9%</p> <span>of sessions booked online</span> </li></ul>A total on slot machine reels
style="slot" spins every digit on a deep reel behind a window frame and lands them from right to left, each with a bounce. A longer stagger makes the wait for the last reel part of the show, which suits fundraising totals and prize pools.
<p class="total" data-dio="counter" data-dio-style="slot" data-dio-stagger="0.25" data-dio-duration="1.6">$4,250,000</p>A year that follows the scroll
With data-dio-trigger="scroll" the count follows the scroll position both ways, from from when the number enters to its value when it reaches the middle of the viewport. On a company timeline it turns the years as the visitor reads.
<p class="year" data-dio="counter" data-dio-trigger="scroll" data-dio-from="1998">2026</p>Notes
The element holds one number, with any text before and after it: $4.2M, 99.9%, 12,480 swimmers or Over 1,200 teams. Only the first number counts; put two stats in two elements. The number keeps the way it is written: its group separators, decimals and leading zeros, as in 1.204.500 or 1 024. A lone point or comma before three digits, as in 3.142, is read with the decimal separator of the page language, so set lang on the page or the element; without one, 12,480 is twelve thousand. A minus sign stays in front as text.
The DOM text never changes, so screen readers, search engines and find in page always see the final value. The drums, reels and cards are drawn only while the digits turn; when they land they fold back into the page and the element is real, selectable text again. Under reduced motion the effect is off and the number shows at once.
Each style draws its parts from the text color and the background behind the element: dark drums on a dark page, light ones on a light page. While they turn, the drums reach about a third of an em above and below the digits, so give the number a line height of 1.3 or some room around it. While the number counts, the element itself is hidden behind its copy, its own background too: put a card’s background on a parent.
from cannot hold more digits than the number, so from="900" works for 1,024 but not for 24. A number on screen when the page opens is hidden until the effect is ready and then counts, as with text-reveal; with the scroll trigger it stays as it is instead.
The canvas cannot draw background-clip: text, text-shadow, -webkit-text-stroke or vertical writing modes the way the browser does. An element with any of them keeps its number as it is, and a warning in development names the reason. style and trigger rebuild the effect when they change.