Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

feat: Counter Animation shows the start line and each count's climb drawn in its tile with a sweeping dot (See inside) and the totals already there beside the same tiles held half a second into their count (Compare)

Claudefc184a4e5cc1
diff --git a/animations/01-scroll-based/counter-animation/index.html b/animations/01-scroll-based/counter-animation/index.html
index afaa2022..eaa7bcba 100644
--- a/animations/01-scroll-based/counter-animation/index.html
+++ b/animations/01-scroll-based/counter-animation/index.html
@@ -34,6 +34,79 @@
/* Tabular figures keep the digits the same width while they change; 26cqw keeps the widest number inside its tile on the narrowest phones */
.tile-num{font-size:min(clamp(28px,4.5vw,44px),26cqw);font-weight:700;font-variant-numeric:tabular-nums;line-height:1;white-space:nowrap;color:var(--ui-accent)}
.tile-label{font-size:12px;color:#8a8a92;text-transform:uppercase;letter-spacing:1.5px}
+ .ov,.cp,.pose,.shade,.sweep,.keys,.ov .nm-l,.mid{display:none}
+ /* The marks of both views live in a layer that stays fixed to the box (a zero-height sticky first child), so no mode adds
+ anything to the scroll length */
+ .stage[data-hb-mode="inside"] .ov,.stage[data-hb-mode="compare"] .ov{display:block;position:sticky;top:0;height:0;z-index:6;pointer-events:none}
+ .stage[data-hb-mode="inside"] .spacer,.stage[data-hb-mode="compare"] .spacer{color:transparent}
+ .ov *{position:absolute}
+ .ov .line{left:0;right:0;top:calc(var(--thr,60) * 1cqh);height:0;border-top:2px dashed #ffd479}
+ .key{display:flex;align-items:center;gap:6px;position:static;padding:4px 8px;border-radius:6px;background:rgba(11,11,13,.9);color:#f4f4f2;
+ font:600 13px/1.25 var(--hb-font);white-space:nowrap}
+ .keys{position:absolute;top:8px;left:8px;flex-direction:column;align-items:flex-start;gap:6px;max-width:calc(100% - 16px)}
+ .key *{position:static;font-weight:inherit}
+ .key .sw{display:block;flex:none;width:16px;height:0;border-top:2px dashed #ffd479}
+ .key .sw.sh{height:10px;border:0;border-radius:2px;background:rgba(110,168,255,.7)}
+ /* See inside: the top edge of the tiles is a bright line that scrolls with them; each tile becomes a small graph of its
+ count (left edge: the first number starts, right edge: the end of the cascade; bottom: zero, dashed line: the total) with
+ a line and dot that sweep across while its number counts. The icon keeps its place, hidden, so the grid keeps its height. */
+ .stage[data-hb-mode="inside"] .ov .line{display:block}
+ .stage[data-hb-mode="inside"] .k-in{display:flex}
+ .stage[data-hb-mode="inside"] #stats{position:relative}
+ .stage[data-hb-mode="inside"] #stats::before{content:"";position:absolute;left:0;right:0;top:0;height:2px;background:#f4f4f2;z-index:3}
+ .stage[data-hb-mode="inside"] .tile{position:relative;overflow:hidden;isolation:isolate}
+ .stage[data-hb-mode="inside"] .tile-icon{visibility:hidden}
+ .stage[data-hb-mode="inside"] .tile-label{color:#d0d0d6}
+ .stage[data-hb-mode="inside"] .shade{display:block;position:absolute;inset:0;width:100%;height:100%;z-index:-1;pointer-events:none}
+ .shade .fill{fill:rgba(110,168,255,.26)}
+ .shade .edge{fill:none;stroke:rgba(150,195,255,.9);stroke-width:1.5;vector-effect:non-scaling-stroke}
+ .shade .tot{stroke:rgba(244,244,242,.45);stroke-width:1;stroke-dasharray:4 3;vector-effect:non-scaling-stroke}
+ .stage[data-hb-mode="inside"] .sweep{display:block;position:absolute;top:0;bottom:0;right:calc(100% - 2px);width:calc(100% - 2px);
+ border-right:2px solid rgba(244,244,242,.75);pointer-events:none;z-index:1}
+ .dotcol{position:absolute;top:0;bottom:0;right:0;width:0}
+ .dotcol::after{content:"";position:absolute;top:0;right:-5px;width:9px;height:9px;margin-top:-4px;border-radius:50%;background:#f4f4f2}
+ /* Compare: the tiles twice, side by side in the same scroll. The grid keeps its Normal height (measured by the script), so
+ the scroll length never changes. The copy is already at its totals; the page's own tiles are the second half, and
+ while no number is counting they rest on a posed moment drawn in elements of their own over the real numbers. */
+ .stage[data-hb-mode="compare"]{overflow-anchor:none}
+ .stage[data-hb-mode="compare"] .ov .line{display:block;left:50%}
+ .stage[data-hb-mode="compare"] .mid{display:block;left:50%;top:0;height:100cqh;border-left:1px solid rgba(244,244,242,.4)}
+ .stage[data-hb-mode="compare"] .ov .nm-l{display:flex;position:absolute;top:8px;left:8px}
+ .stage[data-hb-mode="compare"] .k-r{display:flex;left:calc(50% + 8px);max-width:calc(50% - 16px)}
+ .stage[data-hb-mode="compare"] #stats{grid-template-columns:repeat(4,minmax(0,1fr));grid-template-rows:repeat(2,minmax(0,1fr));
+ height:var(--gh,auto);min-height:0;align-content:stretch;padding-top:52px}
+ .stage[data-hb-mode="compare"] .tile{display:grid;grid-template-columns:minmax(0,1fr);align-content:center;gap:4px;
+ padding:clamp(8px,1.6vw,14px);min-height:0;overflow:hidden}
+ .stage[data-hb-mode="compare"] .cp{display:grid}
+ .stage[data-hb-mode="compare"] .tile-icon{display:none}
+ .stage[data-hb-mode="compare"] .tile-label{letter-spacing:.6px}
+ .stage[data-hb-mode="compare"] .tile-num{grid-area:1/1}
+ .stage[data-hb-mode="compare"].posed .pose{display:block}
+ .stage[data-hb-mode="compare"].posed .tile:not(.cp) .tile-num:not(.pose){color:transparent}
+ @media(min-width:601px){
+ .stage[data-hb-mode="compare"] .k-r{flex-direction:row;flex-wrap:wrap}
+ .stage[data-hb-mode="compare"] .tile:nth-child(-n+2),.stage[data-hb-mode="compare"] .tile:nth-child(5),.stage[data-hb-mode="compare"] .tile:nth-child(6){grid-row:1}
+ .stage[data-hb-mode="compare"] .tile:nth-child(3),.stage[data-hb-mode="compare"] .tile:nth-child(4),.stage[data-hb-mode="compare"] .tile:nth-child(n+7){grid-row:2}
+ .stage[data-hb-mode="compare"] .tile:nth-child(odd){grid-column:1}
+ .stage[data-hb-mode="compare"] .tile:nth-child(even){grid-column:2}
+ .stage[data-hb-mode="compare"] .tile:nth-child(n+5):nth-child(odd){grid-column:3}
+ .stage[data-hb-mode="compare"] .tile:nth-child(n+5):nth-child(even){grid-column:4}
+ }
+ @media(max-width:600px){
+ .key{font-size:12px;padding:3px 6px}
+ .keys{gap:6px}
+ /* A half is one column of its four tiles (a 2x2 half would leave each tile too narrow for its words) */
+ .stage[data-hb-mode="compare"] #stats{grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:repeat(4,minmax(0,1fr));gap:6px}
+ .stage[data-hb-mode="compare"] #stats{padding-top:44px}
+ .stage[data-hb-mode="compare"] .tile{gap:2px;padding:5px 8px}
+ .stage[data-hb-mode="compare"] .tile-num{font-size:min(24px,26cqw)}
+ .stage[data-hb-mode="compare"] .tile:nth-child(4n+1){grid-row:1}
+ .stage[data-hb-mode="compare"] .tile:nth-child(4n+2){grid-row:2}
+ .stage[data-hb-mode="compare"] .tile:nth-child(4n+3){grid-row:3}
+ .stage[data-hb-mode="compare"] .tile:nth-child(4n){grid-row:4}
+ .stage[data-hb-mode="compare"] .tile:nth-child(-n+4){grid-column:1}
+ .stage[data-hb-mode="compare"] .tile:nth-child(n+5){grid-column:2}
+ }
</style>
<link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10">
<script src="../../../assets/js/demo-page.js?v=10" defer></script>
@@ -56,7 +129,24 @@
<span class="hb-num" aria-hidden="true">1</span>
<div><h2 id="hb-watch-title">Scroll it</h2><p>Scroll inside the box, or press Play and it scrolls for you.</p></div>
</div>
+ <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
+ <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
+ <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
+ </div>
<div class="stage" id="stage" tabindex="0" role="region" aria-label="Numbers that count up as they scroll into view">
+ <div class="ov" aria-hidden="true">
+ <i class="line"></i><i class="mid"></i>
+ <div class="keys k-in">
+ <p class="key" data-hb-caption><i class="sw"></i>Dashed line: counting starts here</p>
+ <p class="key" data-hb-caption><i class="sw sh"></i><span>Shade: each count, <b class="spd">about two seconds</b></span></p>
+ </div>
+ <p class="key nm-l" data-hb-caption>Already there</p>
+ <div class="keys k-r">
+ <p class="key" data-hb-caption>Counts up</p>
+ <p class="key" data-hb-caption>Half a second in</p>
+ </div>
+ </div>
<p class="spacer">Scroll down ↓</p>
<div id="stats">
<div class="tile"><div class="tile-icon" aria-hidden="true">◉</div><div class="tile-num" id="cn0" role="img" aria-label="12,847">0</div><div class="tile-label">active users</div></div>
@@ -69,6 +159,10 @@
<button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>Play</button>
<button class="hb-play" type="button" id="btn-top" data-hb-top><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>Back to top</button>
</div>
+ <div class="hb-mode-notes" aria-live="polite">
+ <p data-hb-for="inside" hidden>Inside, the dashed line is where counting starts; each tile's shade shows its count climbing over time, and a line sweeps across as the number counts. Scroll the box or press Play to watch.</p>
+ <p data-hb-for="compare" hidden>Already there shows the totals from the start; Counts up climbs from zero once the tiles reach the dashed line, and rests half a second in. Scroll the box or press Play to watch.</p>
+ </div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
@@ -172,13 +266,73 @@
outExpo:t=>t===1?1:1-Math.pow(2,-10*t),
outBack:t=>{const c=1.70158,c3=c+1;return 1+c3*Math.pow(t-1,3)+c*Math.pow(t-1,2)}
};
- let threshold=60,duration=1800,easing='outCubic',observer,running=false,runId=0;
+ let threshold=60,duration=1800,easing='outCubic',observer,running=false,runId=0,mode='normal';
+ // Where each number is in its count: null before it starts, then 0 to 1 (1 once counted). Both views read it.
+ const prog=TARGETS.map(()=>null);
const cnEls=TARGETS.map((_,i)=>document.getElementById('cn'+i));
+ // Compare's left half: a copy of the tiles at their totals, made once, without ids or names, inert and hidden from screen readers
+ const copies=[...stats.children].map((tile,i)=>{
+ const c=tile.cloneNode(true);
+ c.classList.add('cp');c.inert=true;c.setAttribute('aria-hidden','true');
+ c.querySelectorAll('[id]').forEach(n=>{n.removeAttribute('id');n.removeAttribute('role');n.removeAttribute('aria-label')});
+ c.querySelector('.tile-num').textContent=FORMATS[i](TARGETS[i]);
+ return c;
+ });
+ // The marks inside each real tile: its graph, its sweep and Compare's posed number
+ const marks=[...stats.children].map(tile=>{
+ tile.insertAdjacentHTML('beforeend','<svg class="shade" viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden="true"></svg><span class="sweep" aria-hidden="true"><span class="dotcol"></span></span><span class="tile-num pose" aria-hidden="true"></span>');
+ return {shade:tile.querySelector('.shade'),sweep:tile.querySelector('.sweep'),dot:tile.querySelector('.dotcol'),pose:tile.querySelector('.pose')};
+ });
+ stats.prepend(...copies);
+
+ // The graph: zero at the bottom (94%), the total at 30%, so Springy's overshoot stays inside the tile
+ const BASE=94,TOP=30,POSE_MS=500,FRAME_GAP=200;
+ const gapNow=()=>staggerTog.checked?FRAME_GAP:0;
+ const heightOf=(t)=>BASE-(EASE[easing]||EASE.outCubic)(t)*(BASE-TOP);
+ function drawShades(){
+ const gap=gapNow(),span=duration+3*gap;
+ marks.forEach((m,i)=>{
+ const x0=i*gap/span*100,pts=[];
+ for(let k=0;k<=36;k++)pts.push(((i*gap+duration*k/36)/span*100).toFixed(2)+','+heightOf(k/36).toFixed(2));
+ const curve=`0,${BASE} ${x0.toFixed(2)},${BASE} ${pts.join(' ')} 100,${TOP}`;
+ m.shade.innerHTML=`<polygon class="fill" points="${curve} 100,${BASE}"/><polyline class="edge" points="${curve}"/><line class="tot" x1="0" x2="100" y1="${TOP}" y2="${TOP}"/>`;
+ place(i);
+ });
+ }
+ // A sweep stands where its number stands: across with the time since the number began, up with its share of the total
+ function place(i){
+ const gap=gapNow(),span=duration+3*gap,t=prog[i]===null?0:prog[i];
+ marks[i].sweep.style.transform=`translateX(${((i*gap+duration*t)/span*100).toFixed(2)}%)`;
+ marks[i].dot.style.transform=`translateY(${heightOf(t).toFixed(2)}%)`;
+ }
+ const placeAll=()=>{if(mode==='inside')marks.forEach((_,i)=>place(i))};
+
+ // Compare's posed moment: each number as it is half a second after the first one starts, the cascade applied
+ let poseOn=false,poseTimer=0;
+ function drawPose(){
+ const gap=gapNow();
+ marks.forEach((m,i)=>{
+ const t=Math.max(0,Math.min((POSE_MS-i*gap)/duration,1));
+ m.pose.textContent=FORMATS[i](TARGETS[i]*(EASE[easing]||EASE.outCubic)(t));
+ });
+ }
+ const counting=()=>prog.some(p=>p!==null&&p<1);
+ function setPose(on){if(on!==poseOn){poseOn=on;stage.classList.toggle('posed',on)}}
+ // Lifted while any number is between zero and its total; back about a third of a second after the last count ends (at once
+ // under reduced motion and on entering Compare). It reads the numbers' own state and never starts, stops or writes a count.
+ function syncPose(delay){
+ if(mode!=='compare')return;
+ if(counting()){clearTimeout(poseTimer);poseTimer=0;setPose(false);return}
+ if(poseOn||poseTimer)return;
+ if(delay&&!reduce.matches)poseTimer=setTimeout(()=>{poseTimer=0;setPose(true)},330);else setPose(true);
+ }
+
// A count in progress stops and every number shows zero again
function resetCounters(){
- runId++;running=false;
+ runId++;running=false;prog.fill(null);
cnEls.forEach((el,i)=>{el.textContent=FORMATS[i](0)});
+ placeAll();syncPose(330);
}
// Speed, feel and the cascade are read when a count starts, so a change shows from the next count. A number starts once the
@@ -189,7 +343,8 @@
const start=performance.now(),ms=duration,ef=EASE[easing]||EASE.outCubic;
const gap=staggerTog.checked&&motionOk?200:0;
const begun=[];
- if(motionOk)cnEls.forEach((el,i)=>{el.textContent=FORMATS[i](0)});
+ prog.fill(null);
+ if(motionOk){prog[0]=0;cnEls.forEach((el,i)=>{el.textContent=FORMATS[i](0)});syncPose()}
function frame(now){
if(id!==runId)return;
const boxBottom=stage.getBoundingClientRect().bottom;
@@ -203,8 +358,11 @@
}
const t=motionOk?Math.min((now-begun[i])/ms,1):1;
cnEls[i].textContent=FORMATS[i](target*ef(t));
+ prog[i]=t;
+ if(mode==='inside')place(i);
if(t<1)anyActive=true;
});
+ syncPose(330);
if(anyActive)requestAnimationFrame(frame);else running=false;
}
requestAnimationFrame(frame);
@@ -234,10 +392,55 @@
pick(b.dataset);
}));
}
- choices('start-seg',d=>{threshold=+d.thr;buildObserver()});
- choices('speed-seg',d=>{duration=+d.ms});
- choices('feel-seg',d=>{easing=d.ease});
+ choices('start-seg',d=>{threshold=+d.thr;buildObserver();drawViews()});
+ choices('speed-seg',d=>{duration=+d.ms;drawViews()});
+ choices('feel-seg',d=>{easing=d.ease;drawViews()});
+ staggerTog.addEventListener('change',drawViews);
buildObserver();
+
+ // Compare keeps the grid's Normal height, so the scroll never changes length: measured in Normal (the grid is the same height
+ // in See inside) and, when the box is resized while Compare shows, measured with the mode switched off for a moment
+ function pinHeight(){
+ let h;
+ if(mode==='compare'){
+ const top=stage.scrollTop;
+ stage.setAttribute('data-hb-mode','normal');h=stats.offsetHeight;stage.setAttribute('data-hb-mode','compare');
+ stage.scrollTop=top;
+ }else h=stats.offsetHeight;
+ stats.style.setProperty('--gh',h+'px');
+ }
+ let pinning=0;
+ const queuePin=()=>{if(!pinning)pinning=requestAnimationFrame(()=>{pinning=0;pinHeight()})};
+ new ResizeObserver(queuePin).observe(stage);
+ if(document.fonts)document.fonts.ready.then(queuePin);
+
+ const SECONDS=['one','two','three'];
+ const LABELS={
+ normal:'Numbers that count up as they scroll into view',
+ inside:'The stat tiles with the line where counting starts and each count\'s climb drawn in its tile',
+ compare:'The stat tiles twice: already at their totals, and counting up from zero, held half a second in'
+ };
+ const NOTES={
+ inside:'Shown without counting: the numbers appear at their totals once the tiles reach the dashed line; each tile\'s shade shows how its count would climb over time.',
+ compare:'Shown still, half a second in: Already there shows the totals; in Counts up each number is only part of the way, as it would be while counting.'
+ };
+ const notes=[...document.querySelectorAll('.hb-mode-notes [data-hb-for]')].map(n=>({n,words:n.textContent}));
+ function setNotes(){notes.forEach(o=>{o.n.textContent=reduce.matches?NOTES[o.n.dataset.hbFor]:o.words})}
+ // Everything the views draw follows the settings at once; the live count reads them when the next count starts
+ function drawViews(){
+ stage.style.setProperty('--thr',threshold);
+ stage.querySelector('.spd').textContent='about '+SECONDS[Math.max(0,Math.min(2,Math.round(duration/1000)-1))]+(duration<1500?' second':' seconds');
+ drawShades();drawPose();
+ }
+ document.addEventListener('hb:mode',e=>{
+ mode=e.detail.mode;
+ stage.setAttribute('aria-label',LABELS[mode]);
+ clearTimeout(poseTimer);poseTimer=0;
+ if(mode!=='compare')setPose(false);
+ drawViews();syncPose();
+ });
+ reduce.addEventListener('change',()=>{setNotes();syncPose()});
+ setNotes();drawViews();pinHeight();
</script>
</body>
</html>