Rolling Counter
A number whose digits roll vertically to the new value when it changes.
AI prompt
Build a quantity stepper with a minus button, a number display and a plus button. The display shows a value from 0 to 999 in 40px semibold tabular figures using three digit columns; each column is a 1em tall window with overflow hidden containing a vertical stack of the digits 0 to 9. When the value changes set each column's translateY to minus its digit times 1em with a 320ms cubic-bezier(.3,.9,.3,1) transition, delaying the less significant digits by 30ms each. Hide leading zeros by fading the column to opacity 0 when it is a leading zero. Buttons are 44px circles, disabled at 0 and 999. Holding the plus or minus button repeats after 400ms every 80ms. Put aria-hidden on the columns and show the number in a visually hidden element with role status. Under prefers-reduced-motion remove transitions.About this pattern
Definition
A numeric display where each digit sits in a column of 0 to 9 and slides to the target digit when the value changes, like an odometer.
Purpose & UX principles
- Makes change visible and gives a sense of direction (up or down).
- Suited to quantities, scores and prices that update in place.
- The visual columns are hidden from assistive technology; a live region reads the plain number.
Interaction states
- Idle, rolling, minimum and maximum (buttons disabled), focus-visible on the buttons.
Motion specification
- Each digit column translates 320ms cubic-bezier(.3,.9,.3,1); lower digits start 30ms later for a cascade.
- Reduced motion: digits switch instantly.
Responsive behaviour
- Sized in em units so it scales with the font size.
- Minus and plus buttons are 44px.
Code
<div class="stepper">
<button id="m" aria-label="Decrease">−</button>
<div class="num" aria-hidden="true" id="num"></div>
<span class="sr" role="status" id="sr">12</span>
<button id="p" aria-label="Increase">+</button>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.stepper{display:flex;align-items:center;gap:18px}
.stepper button{width:44px;height:44px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:20px;line-height:1;cursor:pointer;transition:border-color .15s,transform .09s}
.stepper button:hover:not(:disabled){border-color:var(--accent)}
.stepper button:active:not(:disabled){transform:scale(.94)}
.stepper button:disabled{opacity:.35;cursor:default}
.stepper button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.num{display:flex;font-size:40px;font-weight:600;font-variant-numeric:tabular-nums;height:1em;line-height:1;overflow:hidden}
.col{display:block;width:.62em;height:1em;overflow:hidden;transition:opacity .2s}
.col>div{transition:transform .32s cubic-bezier(.3,.9,.3,1)}
.col span{display:block;height:1em;text-align:center}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (prefers-reduced-motion:reduce){.col,.col>div{transition:none}}const num=document.getElementById('num'),sr=document.getElementById('sr'),m=document.getElementById('m'),p=document.getElementById('p');
let v=12;const cols=[];
for(let i=0;i<3;i++){const c=document.createElement('span');c.className='col';const s=document.createElement('div');for(let d=0;d<10;d++){const t=document.createElement('span');t.textContent=d;s.append(t);}c.append(s);num.append(c);cols.push({c,s});}
function render(){const str=String(v).padStart(3,'0');cols.forEach((o,i)=>{o.s.style.transitionDelay=(2-i)*30+'ms';o.s.style.transform='translateY(-'+(+str[i])+'em)';const lead=str.slice(0,i+1).replace(/0/g,'')===''&&i<2;o.c.style.opacity=lead?0:1;});sr.textContent=v;m.disabled=v<=0;p.disabled=v>=999;}
render();
function hold(btn,step){let t,iv;const go=()=>{v=Math.max(0,Math.min(999,v+step));render();};
const stop=()=>{clearTimeout(t);clearInterval(iv);};
btn.addEventListener('pointerdown',()=>{go();t=setTimeout(()=>{iv=setInterval(go,80);},400);});
['pointerup','pointerleave','pointercancel'].forEach(e=>btn.addEventListener(e,stop));
btn.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){if(!e.repeat){e.preventDefault();go();}}});}
hold(m,-1);hold(p,1);One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Rolling Counter — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.stepper{display:flex;align-items:center;gap:18px}
.stepper button{width:44px;height:44px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:20px;line-height:1;cursor:pointer;transition:border-color .15s,transform .09s}
.stepper button:hover:not(:disabled){border-color:var(--accent)}
.stepper button:active:not(:disabled){transform:scale(.94)}
.stepper button:disabled{opacity:.35;cursor:default}
.stepper button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.num{display:flex;font-size:40px;font-weight:600;font-variant-numeric:tabular-nums;height:1em;line-height:1;overflow:hidden}
.col{display:block;width:.62em;height:1em;overflow:hidden;transition:opacity .2s}
.col>div{transition:transform .32s cubic-bezier(.3,.9,.3,1)}
.col span{display:block;height:1em;text-align:center}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (prefers-reduced-motion:reduce){.col,.col>div{transition:none}}
</style></head>
<body><div class="stage">
<div class="stepper">
<button id="m" aria-label="Decrease">−</button>
<div class="num" aria-hidden="true" id="num"></div>
<span class="sr" role="status" id="sr">12</span>
<button id="p" aria-label="Increase">+</button>
</div>
</div>
<script>
const num=document.getElementById('num'),sr=document.getElementById('sr'),m=document.getElementById('m'),p=document.getElementById('p');
let v=12;const cols=[];
for(let i=0;i<3;i++){const c=document.createElement('span');c.className='col';const s=document.createElement('div');for(let d=0;d<10;d++){const t=document.createElement('span');t.textContent=d;s.append(t);}c.append(s);num.append(c);cols.push({c,s});}
function render(){const str=String(v).padStart(3,'0');cols.forEach((o,i)=>{o.s.style.transitionDelay=(2-i)*30+'ms';o.s.style.transform='translateY(-'+(+str[i])+'em)';const lead=str.slice(0,i+1).replace(/0/g,'')===''&&i<2;o.c.style.opacity=lead?0:1;});sr.textContent=v;m.disabled=v<=0;p.disabled=v>=999;}
render();
function hold(btn,step){let t,iv;const go=()=>{v=Math.max(0,Math.min(999,v+step));render();};
const stop=()=>{clearTimeout(t);clearInterval(iv);};
btn.addEventListener('pointerdown',()=>{go();t=setTimeout(()=>{iv=setInterval(go,80);},400);});
['pointerup','pointerleave','pointercancel'].forEach(e=>btn.addEventListener(e,stop));
btn.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){if(!e.repeat){e.preventDefault();go();}}});}
hold(m,-1);hold(p,1);
</script>
</body></html>