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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: Notification Badge Pulse shows the bell and its badge drawn big with dashed outlines for its rest size, its biggest and where the halo ends (See inside) and badges that pulse forever beside badges that stop after three (Compare)
matin48953e4a4b25
diff --git a/animations/04-micro-interactions/badge-pulse/index.html b/animations/04-micro-interactions/badge-pulse/index.htmlindex 9e62cc3e..275476bc 100644--- a/animations/04-micro-interactions/badge-pulse/index.html+++ b/animations/04-micro-interactions/badge-pulse/index.html@@ -23,11 +23,11 @@<style>:root{--ui-accent:#ff9d5c;--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-muted:#8a8a92;--pulse-dur:1500ms;--pulse-scale:1.3;--badge-color:#f85149}*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}- .stage{display:flex;align-items:center;justify-content:center;padding:16px;background:#0b0b0d}+ .stage{position:relative;display:flex;align-items:center;justify-content:center;padding:16px;background:#0b0b0d}.icon-row{display:flex;gap:clamp(16px,6vw,40px);align-items:flex-end}/* Icon base */.icon-wrap{display:flex;flex-direction:column;align-items:center;gap:12px;position:relative}- .icon-btn{width:64px;height:64px;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:28px;position:relative;cursor:pointer;transition:border-color .15s}+ .icon-btn,.z-tile{width:64px;height:64px;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:28px;position:relative;cursor:pointer;transition:border-color .15s}.icon-round{border-radius:50%}@media(hover:hover){.icon-btn:hover{border-color:var(--ui-accent)}}.icon-label{font-size:11px;color:var(--ui-muted);text-align:center}@@ -52,6 +52,60 @@.fade-mode .badge,.fade-mode .dot-badge,.fade-mode .badge::after,.fade-mode .dot-badge::after{animation-iteration-count:3!important;animation-fill-mode:forwards!important}/* Status dot (online) */.status-dot{position:absolute;bottom:-2px;right:-2px;width:16px;height:16px;border-radius:50%;background:#56d364;border:2px solid var(--bg);animation:badge-scale var(--pulse-dur) ease-in-out infinite}+ /* See inside and Compare. Their parts stay in the stage, unseen and out of the way, outside their modes, so their pulses run+ beside the real ones and Pause, Slow motion and the Speed change reach them. */+ .zoom,.cmp-left,.cap-n{position:absolute;left:0;top:0;visibility:hidden;pointer-events:none}+ .cap{display:flex;align-items:center;gap:7px;padding:3px 8px;border:1px solid rgba(255,255,255,.14);border-radius:6px;background:rgba(8,8,10,.92);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);text-align:center}+ .cap[hidden]{display:none}+ .cap-grow::before,.cap-ring::before{content:"";flex:none;width:12px;height:12px;border-radius:50%;border:2px dashed var(--ui-accent)}+ .cap-ring::before{border-color:#f4f4f2}+ /* See inside: the bell zoomed in, every size of the tile and the badge multiplied by --z (laid out big, not scaled up, so the+ number and the outlines stay sharp). The outlines come from the badge: its rest size, its biggest and where the halo ends. */+ .zdraw{position:relative;flex:none;--z:2;width:calc(94px*var(--z));height:calc(94px*var(--z))}+ .zdraw .z-tile{position:absolute;left:0;bottom:0;width:calc(64px*var(--z));height:calc(64px*var(--z));border-radius:calc(12px*var(--z));border-width:calc(1px*var(--z));font-size:calc(28px*var(--z))}+ .zdraw .badge,.stage[data-hb-mode="compare"] .badge{top:calc(-8px*var(--z));right:calc(-8px*var(--z));min-width:calc(22px*var(--z));height:calc(22px*var(--z));border-radius:calc(11px*var(--z));font-size:calc(11px*var(--z));padding:0 calc(5px*var(--z));border-width:calc(2px*var(--z))}+ .zdraw .badge::after,.stage[data-hb-mode="compare"] .badge::after,.stage[data-hb-mode="compare"] .dot-badge::after{inset:calc(-4px*var(--z))}+ .zmark{position:absolute;top:calc(-8px*var(--z));right:calc(-8px*var(--z));width:calc(22px*var(--z));height:calc(22px*var(--z))}+ .zmark i{position:absolute;left:50%;top:50%;border:2px dashed #f4f4f2;border-radius:50%;transform:translate(-50%,-50%)}+ .zr{width:100%;height:100%}+ .zmark .zb{width:calc(100%*var(--pulse-scale));height:calc(100%*var(--pulse-scale));border-color:var(--ui-accent)}+ .zh{width:calc((100% + 8px*var(--z))*2.2);height:calc((100% + 8px*var(--z))*2.2);opacity:.7}+ .pulse-halo .zb,.pulse-scale .zh{display:none}+ .stage[data-hb-mode="inside"]{gap:clamp(24px,5vw,64px)}+ .stage[data-hb-mode="inside"] .zoom{position:static;visibility:visible;display:flex;align-items:center;gap:clamp(20px,3vw,40px)}+ .stage[data-hb-mode="inside"] .zcaps{display:flex;flex-direction:column;align-items:flex-start;gap:10px;max-width:300px}+ /* Under 1000px the real bell stands alone on the left, the zoomed one beside it, a caption above and two below. The inbox and+ the profile step aside, out of the flow but still drawn, because they pulse. */+ @media(max-width:999px){+ .stage[data-hb-mode="inside"]{display:grid;grid-template-columns:auto minmax(0,1fr);grid-template-rows:auto minmax(0,1fr) auto auto;gap:8px clamp(22px,7vw,40px);padding:12px;justify-items:center}+ .stage[data-hb-mode="inside"] .zoom,.stage[data-hb-mode="inside"] .zcaps{display:contents}+ .stage[data-hb-mode="inside"] .icon-wrap:nth-child(n+2){position:absolute;left:0;top:0;visibility:hidden}+ .stage[data-hb-mode="inside"] .icon-row{grid-area:2/1}+ .stage[data-hb-mode="inside"] .zdraw{grid-area:2/2}+ .stage[data-hb-mode="inside"] .cap-grow{grid-area:1/1/2/3}+ .stage[data-hb-mode="inside"] .cap-ring{grid-area:3/1/4/3}+ .stage[data-hb-mode="inside"] .cap-speed{grid-area:4/1/5/3}+ }+ /* Compare: the same two icons twice, side by side. Left, a copy that pulses forever (half a pulse ahead, so a still catches it+ part way through); right, the real ones, held to three pulses without restarting them. While paused the right ones rest. */+ .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:minmax(0,1fr);padding:0;--ct:64px;--z:1}+ .stage[data-hb-mode="compare"] .cmp-left{position:static;visibility:visible;grid-area:1/1;border-right:1px solid rgba(255,255,255,.16)}+ .stage[data-hb-mode="compare"] .icon-row{grid-area:1/2}+ .stage[data-hb-mode="compare"] .cmp-left,.stage[data-hb-mode="compare"] .icon-row{display:flex;align-items:center;justify-content:center;gap:clamp(12px,4vw,40px);padding:46px 4px 0;min-width:0;overflow:hidden}+ .stage[data-hb-mode="compare"] .icon-wrap:nth-child(3){position:absolute;left:0;top:0;visibility:hidden}+ /* On wider boxes both halves are drawn 1.5 times bigger (sizes multiplied, as in See inside), so the pulse reads from a distance */+ .stage[data-hb-mode="compare"] .icon-btn,.stage[data-hb-mode="compare"] .cmp-left .z-tile{width:calc(var(--ct)*var(--z));height:calc(var(--ct)*var(--z));border-radius:calc(12px*var(--z));font-size:calc(28px*var(--z))}+ .stage[data-hb-mode="compare"] .dot-badge{top:calc(-4px*var(--z));right:calc(-4px*var(--z));width:calc(14px*var(--z));height:calc(14px*var(--z));border-width:calc(2px*var(--z))}+ @media(min-width:700px){.stage[data-hb-mode="compare"]{--z:1.5}}+ .stage[data-hb-mode="compare"] .cap-n{visibility:visible}+ .cap-n{top:10px;white-space:nowrap;transform:translateX(-50%)}+ .cap-na{left:25%}+ .cap-nb{left:75%}+ @media(max-width:350px){.stage[data-hb-mode="compare"]{--ct:54px}.cap{font-size:12px}}+ .stage[data-hb-mode="compare"] .cmp-left .badge,.stage[data-hb-mode="compare"] .cmp-left .dot-badge{animation-delay:calc(var(--pulse-dur)*-.5)}+ .stage[data-hb-mode="compare"] .cmp-left .badge::after,.stage[data-hb-mode="compare"] .cmp-left .dot-badge::after{animation-delay:calc(var(--pulse-dur)*-.25)}+ .stage[data-hb-mode="compare"] .icon-row .badge,.stage[data-hb-mode="compare"] .icon-row .dot-badge,.stage[data-hb-mode="compare"] .icon-row .badge::after,.stage[data-hb-mode="compare"] .icon-row .dot-badge::after{animation-iteration-count:3;animation-fill-mode:forwards}+ .stage[data-hb-mode="compare"].hb-paused .icon-row .badge,.stage[data-hb-mode="compare"].hb-paused .icon-row .dot-badge,.stage[data-hb-mode="compare"].hb-paused .icon-row .badge::after,.stage[data-hb-mode="compare"].hb-paused .icon-row .dot-badge::after{animation-delay:calc(var(--pulse-dur)*-3)}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10"><script src="../../../assets/js/demo-page.js?v=10" defer></script>@@ -74,6 +128,11 @@<span class="hb-num" aria-hidden="true">1</span><div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it, or click an icon to clear its badge; Reset brings the badges back.</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 hb-dots"><div class="icon-row" id="icon-row"><div class="icon-wrap">@@ -98,12 +157,27 @@<div class="icon-label">Online</div></div></div>+ <div class="zoom" aria-hidden="true" inert>+ <div class="zdraw"></div>+ <div class="zcaps">+ <p class="cap cap-grow" data-hb-caption></p>+ <p class="cap cap-ring" data-hb-caption hidden>A soft ring spreads to this circle as it fades</p>+ <p class="cap cap-speed" data-hb-caption></p>+ </div>+ </div>+ <div class="cmp-left" aria-hidden="true" inert></div>+ <p class="cap cap-n cap-na" data-hb-caption aria-hidden="true">Pulses forever</p>+ <p class="cap cap-n cap-nb" data-hb-caption aria-hidden="true">Stops after three</p></div><div class="hb-player"><button class="hb-play" type="button" id="btn-pause" data-hb-pause="css" data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button><button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</button><label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label></div>+ <div class="hb-mode-notes" aria-live="polite">+ <p data-hb-for="inside" hidden>Inside, the bell's badge is shown bigger: with Grow it swells from the inner outline to the outer one and back; with Ring a soft halo spreads to the dashed circle.</p>+ <p data-hb-for="compare" hidden>Pulses forever: the badges never stop, even after you have seen them; Stops after three: they pulse three times, then rest. Press Reset to watch the three pulses again.</p>+ </div></section><section class="hb-step hb-try" aria-labelledby="hb-try-title">@@ -206,25 +280,123 @@const icons=[...document.querySelectorAll('.icon-btn')];// While its badge shows, an icon's label says what the badge says ("Notifications, 3 new"); the part before the comma names the icon aloneconst fullLabel=new Map(icons.map(ic=>[ic,ic.getAttribute('aria-label')]));- let currentStyle='pulse-scale',pulseMs=1500;+ let currentStyle='pulse-scale',pulseMs=1500,growK=1.3;+ const reduceMQ=matchMedia('(prefers-reduced-motion: reduce)'),phoneMQ=matchMedia('(max-width:600px),(max-height:500px)');+ const $=s=>stage.querySelector(s),slowTog=document.getElementById('slow-tog'),fadeTog=document.getElementById('fade-tog');- // Taking the pulse class off, reading the layout and putting it back starts the bell's and the inbox's animations again- // from their first frame (on a paused stage they wait there until Play). The online dot has no pulse class and keeps going.- function applyStyle(){- icons.forEach((ic,i)=>{- if(i===2)return;- ic.classList.remove('pulse-scale','pulse-halo','pulse-both');- void ic.offsetWidth;- ic.classList.add(currentStyle);+ // See inside and Compare: copies made once, here, after `icons` was taken, as inert tiles of another class (z-tile) with no+ // ids, role, name or tab stop. Compare's left half copies the bell and the inbox; See inside draws the bell again, zoomed.+ const left=$('.cmp-left');+ const lefts=icons.slice(0,2).map(ic=>{+ const wrap=ic.parentNode.cloneNode(true),tile=wrap.querySelector('.icon-btn');+ wrap.querySelectorAll('[id]').forEach(e=>e.removeAttribute('id'));+ tile.classList.replace('icon-btn','z-tile');+ ['role','tabindex','aria-label'].forEach(a=>tile.removeAttribute(a));+ left.appendChild(wrap);+ return tile;+ });+ const zt=document.createElement('div');+ zt.className='z-tile '+currentStyle;+ zt.innerHTML='🔔<div class="badge">'+icons[0].querySelector('.badge').textContent+'</div><div class="zmark"><i class="zr"></i><i class="zb"></i><i class="zh"></i></div>';+ $('.zdraw').appendChild(zt);+ const pulsers=[...icons.slice(0,2),zt,...lefts];++ // Under reduced motion the loop starts paused, so both views are still pictures and their notes say so, but only while the+ // still is the posed one: once Play has moved it on, the notes say the markup's own words, until a pause, a mode press or a+ // restart poses it again. The shared script sends hb:pause on every press of Pause or Play, and once at boot under reduced+ // motion. A note is rewritten only when its words change, as its box reads changes out loud.+ const notes=[...document.querySelectorAll('[data-hb-for]')],moving=notes.map(n=>n.textContent),still=[+ "Inside, the bell's badge is shown bigger and still, part way through a pulse: with Grow at its biggest, inside the outer outline; with Ring its halo part way out.",+ 'Shown still: Pulses forever is caught mid-pulse, as it would be all the time; Stops after three has pulsed three times and now rests.'];+ let paused=reduceMQ.matches,posedIn=false,posedCmp=false;+ function describe(){+ const shown=[posedIn,posedCmp].map(p=>reduceMQ.matches&&paused&&p);+ notes.forEach((n,i)=>{const words=shown[i]?still[i]:moving[i];if(n.textContent!==words)n.textContent=words});+ }+ describe();+ // Paused, a view rests on a telling moment, as a mode press, a restart or a Pause deals it. Compare's left copies go back to+ // time zero, which their negative delay makes half a pulse in (a quarter for the halo), and its right half rests by its own+ // rule. The zoomed bell goes half a pulse in (a quarter for the halo) on a mode press or a restart, not on a press of Pause, so+ // Pause still freezes it where it is; Play puts it in step with the real bell again.+ function pose(fromPause){+ const m=stage.dataset.hbMode;+ if(paused&&m==='compare'){+ left.getAnimations({subtree:true}).forEach(a=>{a.currentTime=0});+ posedCmp=true;+ }+ if(paused&&m==='inside'&&!fromPause){+ zt.getAnimations({subtree:true}).forEach(a=>{a.currentTime=(a.animationName==='badge-scale'?.5:.25)*a.effect.getComputedTiming().duration});+ posedIn=true;+ }+ describe();+ }+ function inStep(){+ const real=icons[0].getAnimations({subtree:true});+ zt.getAnimations({subtree:true}).forEach(z=>{+ const r=real.find(a=>a.animationName===z.animationName&&a.effect.pseudoElement===z.effect.pseudoElement);+ if(r)z.currentTime=r.currentTime;});}+ document.addEventListener('hb:pause',e=>{+ paused=e.detail.paused;+ if(paused)pose(true);+ else{posedIn=posedCmp=false;inStep()}+ describe();+ });+ reduceMQ.addEventListener('change',describe);++ // The captions of See inside say what the zoomed badge shows now, from the page's own settings+ const capGrow=$('.cap-grow'),capRing=$('.cap-ring'),capSpeed=$('.cap-speed');+ const growWords={1.15:'a little bigger',1.3:'about a third bigger',1.5:'half again as big'};+ const everyWords={2400:['every two and a half seconds','about every seven seconds'],1500:['every second and a half','every four and a half seconds'],900:['about every second','about every three seconds']};+ function words(){+ capGrow.textContent='Grows '+growWords[growK]+', then back';+ capSpeed.textContent=fadeTog.checked?'Three pulses, then it rests':'One pulse '+everyWords[pulseMs][+slowTog.checked];+ capGrow.hidden=currentStyle==='pulse-halo';+ capRing.hidden=currentStyle==='pulse-scale';+ fit();+ }+ // The zoom is the biggest picture (2.5 times, 2 on phones) in which the zoomed bell and every caption sit inside the box and+ // clear of each other and of the real bell; it is measured again on a mode, a setting, a resize and when the font arrives.+ function fit(){+ if(stage.dataset.hbMode!=='inside')return;+ const draw=$('.zdraw'),box=stage.getBoundingClientRect();+ const L=box.left+stage.clientLeft,T=box.top+stage.clientTop,R=L+stage.clientWidth,B=T+stage.clientHeight;+ const parts=[...stage.querySelectorAll('.zdraw,.zcaps .cap,#icon-row')].filter(e=>e.getClientRects().length);+ const clear=()=>{+ const rs=parts.map(e=>e.getBoundingClientRect());+ return rs.every((r,i)=>r.left>=L-.5&&r.right<=R+.5&&r.top>=T-.5&&r.bottom<=B+.5&&+ rs.every((q,j)=>j<=i||r.right<=q.left||q.right<=r.left||r.bottom<=q.top||q.bottom<=r.top));+ };+ for(let z=phoneMQ.matches?2:2.5;z>=1.2;z=+(z-.1).toFixed(2)){+ draw.style.setProperty('--z',z);+ if(clear())return;+ }+ }+ document.addEventListener('hb:mode',()=>{pose();words()});+ slowTog.addEventListener('change',words);+ let resizeTimer;+ addEventListener('resize',()=>{clearTimeout(resizeTimer);resizeTimer=setTimeout(fit,100)});+ if(document.fonts)document.fonts.ready.then(fit);++ // Taking the pulse class off, reading the layout and putting it back starts the pulses again from their first frame (on a+ // paused stage they wait there until Play): the bell's and the inbox's, and the copies that follow them. The online dot has+ // no pulse class and keeps going. The halves of Compare restart together, so the three pulses show beside the endless ones.+ function applyStyle(){+ pulsers.forEach(t=>t.classList.remove('pulse-scale','pulse-halo','pulse-both'));+ void stage.offsetWidth;+ pulsers.forEach(t=>t.classList.add(currentStyle));+ pose();+ }// Clicking an icon, or pressing Enter or Space on it, clears its badge and its label stops announcing it; Reset brings both back at once.- icons.forEach(ic=>{+ // Compare's left copy of the icon clears its badge too.+ icons.forEach((ic,i)=>{const dismiss=()=>{const b=ic.querySelector('.badge,.dot-badge');if(!b)return;b.style.display='none';+ if(lefts[i])lefts[i].querySelector('.badge,.dot-badge').style.display='none';ic.setAttribute('aria-label',fullLabel.get(ic).split(',')[0]);};ic.addEventListener('click',dismiss);@@ -244,7 +416,7 @@}));}// Pressing the style that is already chosen changes nothing; it must not restart the pulses- choices('style-seg',d=>{if(d.style===currentStyle)return;currentStyle=d.style;applyStyle()});+ choices('style-seg',d=>{if(d.style===currentStyle)return;currentStyle=d.style;applyStyle();words()});// A new --pulse-dur keeps the time already played, so every pulse would jump to another point of its cycle. Scaling each// animation's time by the same factor as its length keeps the pulses where they are: moving, paused or finished (the fill mode of// "Stops after three pulses" keeps a finished pulse in the list). The times are read first, the new length goes in and is flushed,@@ -257,13 +429,16 @@void stage.offsetWidth;pulses.forEach(([a,t])=>{if(t!=null)a.currentTime=t*next/pulseMs});pulseMs=next;+ words();});- choices('grow-seg',d=>{vars.setProperty('--pulse-scale',d.scale)});+ choices('grow-seg',d=>{vars.setProperty('--pulse-scale',d.scale);growK=+d.scale;words()});choices('color-sw',d=>{vars.setProperty('--badge-color',d.c)});- document.getElementById('fade-tog').addEventListener('change',e=>{- icons.forEach(ic=>ic.classList.toggle('fade-mode',e.target.checked));+ fadeTog.addEventListener('change',e=>{+ [...icons,zt].forEach(t=>t.classList.toggle('fade-mode',e.target.checked));applyStyle();+ words();});+ words();</script></body></html>