Odometer counter
Numbers that roll into place digit by digit like a mechanical counter when they scroll into view.
"use client";
import { useEffect, useRef, useState } from "react";
import "./odometer.css";
type OdometerProps = {
value: number;
/** Text before / after the number, e.g. "$" or "+". */
prefix?: string;
suffix?: string;
/** Formats the number, e.g. 12400 -> "12,400". */
locale?: string;
};
/**
* Odometer
* Each digit is a vertical strip 0–9 that slides to the right digit.
* Digits start at 0 and roll when the number scrolls into view.
*/
export function Odometer({
value,
prefix = "",
suffix = "",
locale = "en-US",
}: OdometerProps) {
const ref = useRef<HTMLSpanElement>(null);
const [shown, setShown] = useState(false);
const [run, setRun] = useState(0); // bump to replay
const text = value.toLocaleString(locale);
useEffect(() => {
const el = ref.current;
if (!el) return;
const io = new IntersectionObserver(
([e]) => e.isIntersecting && setShown(true),
{ threshold: 0.5 },
);
io.observe(el);
return () => io.disconnect();
}, []);
const replay = () => {
setShown(false);
requestAnimationFrame(() => requestAnimationFrame(() => setShown(true)));
setRun((r) => r + 1);
};
return (
<span ref={ref} className="gu-odo" onClick={replay} data-run={run}>
<span className="gu-odo__sr">{prefix + text + suffix}</span>
<span className="gu-odo__digits" aria-hidden="true">
{prefix}
{[...text].map((ch, i) => {
if (!/\d/.test(ch)) return <span key={i}>{ch}</span>;
const digit = Number(ch);
return (
<span key={i} className="gu-odo__window">
<span
className="gu-odo__strip"
style={{
transform: `translateY(${shown ? -digit * 10 : 0}%)`,
transitionDelay: `${i * 70}ms`,
}}
>
{"0123456789".split("").map((d) => (
<span key={d}>{d}</span>
))}
</span>
</span>
);
})}
{suffix}
</span>
</span>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Odometer counter
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
An odometer-style number: every digit is a little drum that rolls up to its value when the number scrolls into view, with a slight left-to-right stagger. Great for stats rows.
## Layout and look
- Inherit font size and color from the parent. Use tabular numbers.
- Each digit: an inline window exactly 1em tall with overflow hidden and a vertical mask (transparent → black 12% → black 88% → transparent) so digits fade at the edges.
- Inside the window: a vertical strip of the digits 0–9, each 1em tall.
- Non-digit characters (commas, prefix like "$", suffix like "+" or "%") render as plain text.
- Demo: three stats, value at clamp(2.5rem, 7vw, 4.5rem) weight 900 letter-spacing -0.04em, label below in 14px muted text.
## Behavior and motion
- Format the value with toLocaleString (e.g. 12,480).
- Strips start at 0. When 50% of the number is visible (IntersectionObserver), translate each strip to -(digit × 10)% over 1.4s with cubic-bezier(0.16, 1, 0.3, 1), each digit delayed 70ms more than the previous.
- Clicking or tapping the number resets to 0 and rolls again.
## Accessibility
- A visually hidden span holds the full readable number ("$0", "12,480+"); the animated digits are aria-hidden.
- In the demo, use a <dl> with <dt> labels and <dd> values.
- prefers-reduced-motion: no transition; show the final number immediately.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### Odometer.tsx
```tsx
"use client";
import { useEffect, useRef, useState } from "react";
import "./odometer.css";
type OdometerProps = {
value: number;
/** Text before / after the number, e.g. "$" or "+". */
prefix?: string;
suffix?: string;
/** Formats the number, e.g. 12400 -> "12,400". */
locale?: string;
};
/**
* Odometer
* Each digit is a vertical strip 0–9 that slides to the right digit.
* Digits start at 0 and roll when the number scrolls into view.
*/
export function Odometer({
value,
prefix = "",
suffix = "",
locale = "en-US",
}: OdometerProps) {
const ref = useRef<HTMLSpanElement>(null);
const [shown, setShown] = useState(false);
const [run, setRun] = useState(0); // bump to replay
const text = value.toLocaleString(locale);
useEffect(() => {
const el = ref.current;
if (!el) return;
const io = new IntersectionObserver(
([e]) => e.isIntersecting && setShown(true),
{ threshold: 0.5 },
);
io.observe(el);
return () => io.disconnect();
}, []);
const replay = () => {
setShown(false);
requestAnimationFrame(() => requestAnimationFrame(() => setShown(true)));
setRun((r) => r + 1);
};
return (
<span ref={ref} className="gu-odo" onClick={replay} data-run={run}>
<span className="gu-odo__sr">{prefix + text + suffix}</span>
<span className="gu-odo__digits" aria-hidden="true">
{prefix}
{[...text].map((ch, i) => {
if (!/\d/.test(ch)) return <span key={i}>{ch}</span>;
const digit = Number(ch);
return (
<span key={i} className="gu-odo__window">
<span
className="gu-odo__strip"
style={{
transform: `translateY(${shown ? -digit * 10 : 0}%)`,
transitionDelay: `${i * 70}ms`,
}}
>
{"0123456789".split("").map((d) => (
<span key={d}>{d}</span>
))}
</span>
</span>
);
})}
{suffix}
</span>
</span>
);
}
```
### odometer.css
```css
.gu-odo {
display: inline-block;
font-variant-numeric: tabular-nums;
cursor: pointer;
}
.gu-odo__digits {
display: inline-flex;
align-items: baseline;
}
/* A window exactly one digit tall... */
.gu-odo__window {
display: inline-block;
height: 1em;
line-height: 1;
overflow: hidden;
/* soft top/bottom fade sells the "rolling drum" */
mask-image: linear-gradient(transparent, #000 12%, #000 88%, transparent);
-webkit-mask-image: linear-gradient(
transparent,
#000 12%,
#000 88%,
transparent
);
}
/* ...over a strip of 0–9 that slides up. */
.gu-odo__strip {
display: flex;
flex-direction: column;
transition: transform 1.4s cubic-bezier(0.16, 1, 0.3, 1);
}
.gu-odo__strip > span {
height: 1em;
}
.gu-odo__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
.gu-odo__strip {
transition: none;
}
}
```
You might also want
Next piece: Split text revealMarker highlight
Animations, beginner
Scroll-speed marquee
Animations, intermediate
Split text reveal
Animations, intermediate, needs motion