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: Copy to Clipboard shows the button twice as large with its shared spots outlined and a bar for how long Copied! stays (See inside) and a button that gives no sign beside one that confirms (Compare)
matinec9a63d34dee
diff --git a/animations/04-micro-interactions/copy-to-clipboard/index.html b/animations/04-micro-interactions/copy-to-clipboard/index.htmlindex dcd43488..060dfab0 100644--- a/animations/04-micro-interactions/copy-to-clipboard/index.html+++ b/animations/04-micro-interactions/copy-to-clipboard/index.html@@ -56,6 +56,63 @@.fail{position:absolute;top:calc(100% + 12px);left:50%;transform:translateX(-50%);width:max-content;max-width:min(300px,80vw);font-size:12px;line-height:1.4;text-align:center;color:var(--ui-text)}.sr-only{position:absolute;width:1px;height:1px;padding:0;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}@media(prefers-reduced-motion:reduce){.copy-btn,.copy-btn::after,.ic svg,.ic .chk path,.labels span{transition:none!important;animation:none!important}}+ /* See inside and Compare, drawn around the real button from the page's own numbers (the script says how) */+ .stage{position:relative}+ .views,.leads,.tbar,.cmp-one,.cmp-name,.pose{display:none}+ .cap{padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.88);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);text-wrap:balance}+ .leads{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}+ .leads line{stroke:rgba(244,244,242,.75);stroke-width:1.5}+ .leads .l-flash{opacity:0}+ .stage[data-hb-mode="inside"]{padding:clamp(10px,2vw,16px)}+ .stage[data-hb-mode="inside"] .copy-wrap{display:grid;width:100%;grid-template-columns:minmax(0,1fr) auto minmax(0,1fr);align-items:center;justify-items:center;gap:6px 12px}+ .stage[data-hb-mode="inside"] .copy-row,.stage[data-hb-mode="inside"] .views{display:contents}+ .stage[data-hb-mode="inside"] .leads{display:block}+ .stage[data-hb-mode="inside"] .code-box{grid-area:1/2}+ .stage[data-hb-mode="inside"] .copy-btn{grid-area:2/2;margin:var(--my,22px) var(--mx,50px);transform:scale(2)}+ .stage[data-hb-mode="inside"] .ic::after,.stage[data-hb-mode="inside"] .labels::after{content:"";position:absolute;inset:-3px;border:1px dashed rgba(244,244,242,.9);border-radius:4px}+ .stage[data-hb-mode="inside"] .views .cap{display:block;max-width:100%}+ .stage[data-hb-mode="inside"] .c-icons{grid-area:2/1;justify-self:end;text-align:right}+ .stage[data-hb-mode="inside"] .c-words{grid-area:2/3;justify-self:start}+ .stage[data-hb-mode="inside"] .c-flash{grid-area:1/3;justify-self:start;align-self:end;opacity:0}+ .stage[data-hb-mode="inside"] .c-swap{grid-area:3/1/4/-1;text-align:center}+ .stage[data-hb-mode="inside"] .tbar{display:block;grid-area:4/1/5/-1;position:relative;width:min(100%,320px);height:12px}+ .stage[data-hb-mode="inside"] .c-bar{grid-area:5/1/6/-1;text-align:center}+ .stage[data-hb-mode="inside"] .fail{position:static;transform:none;grid-area:3/1/4/-1;justify-self:center;max-width:100%;text-align:center}+ .tbar i{position:absolute;left:0;right:0;top:3px;height:6px;border-radius:3px}+ .t-track{background:rgba(244,244,242,.2)}+ .t-bar{background:var(--ok);transform-origin:0 50%}+ .t-mark{right:auto;left:-6px;top:0;width:12px;height:12px;border-radius:50%;background:#f4f4f2}+ .stage[data-hb-mode="inside"]:has(.copy-btn.copied) .c-flash,.stage[data-hb-mode="inside"]:has(.copy-btn.copied) .l-flash{animation:flashcap 900ms linear both}+ @keyframes flashcap{0%,65%{opacity:1}100%{opacity:0}}+ @media(max-width:600px),(max-height:500px){.stage[data-hb-mode="inside"] .views .c-flash,.stage[data-hb-mode="inside"] .l-flash{display:none}}+ .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));align-items:center;justify-items:center;padding:0 8px}+ .stage[data-hb-mode="compare"]::after{content:"";position:absolute;top:0;bottom:0;left:50%;border-left:1px solid rgba(244,244,242,.12);pointer-events:none}+ .stage[data-hb-mode="compare"] .cmp-one,.stage[data-hb-mode="compare"] .copy-wrap{margin-top:30px;max-width:100%}+ .stage[data-hb-mode="compare"] .cmp-one{display:block}+ .stage[data-hb-mode="compare"] .cmp-name{display:block;position:absolute;top:10px;left:12px}+ .stage[data-hb-mode="compare"] .cmp-name.second{left:calc(50% + 12px)}+ .stage[data-hb-mode="compare"] .fail{top:calc(100% + 6px)}+ .cmp-one .copy-btn{cursor:default;text-align:center}+ @media(max-width:340px){.stage[data-hb-mode="compare"] .code-box{padding:8px 10px}.stage[data-hb-mode="compare"] .code{font-size:17px}}+ @media(max-width:600px),(max-height:500px){.stage[data-hb-mode="compare"] .copy-row{gap:8px}.stage[data-hb-mode="compare"] .code-box{padding:8px 12px}}+ .stage[data-hb-mode="compare"] .pose:not([hidden]){display:inline-flex;position:absolute;pointer-events:none;text-align:center}+ .pose.snap,.pose.snap *,.pose.snap::after{transition:none!important;animation:none!important}+ .copy-wrap:has(.copy-btn:focus-visible) .pose{outline:2px solid var(--hb-accent);outline-offset:-3px}+ /* phones: both views, after the rules they change */+ @media(max-width:600px){+ .stage[data-hb-mode="inside"] .copy-wrap{grid-template-columns:minmax(0,1fr)}+ .stage[data-hb-mode="inside"] .code-box{display:none}+ .stage[data-hb-mode="inside"] .c-icons{grid-area:1/1;justify-self:center;text-align:center}+ .stage[data-hb-mode="inside"] .copy-btn{grid-area:2/1}+ .stage[data-hb-mode="inside"] .c-words{grid-area:3/1;justify-self:center;text-align:center}+ .stage[data-hb-mode="inside"] .c-swap,.stage[data-hb-mode="inside"] .fail{grid-area:4/1}+ .stage[data-hb-mode="inside"] .tbar{grid-area:5/1;justify-self:center}+ .stage[data-hb-mode="inside"] .c-bar{grid-area:6/1}+ .stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}+ .stage[data-hb-mode="compare"]::after{top:50%;bottom:auto;left:0;right:0;border-left:0;border-top:1px solid rgba(244,244,242,.12)}+ .stage[data-hb-mode="compare"] .cmp-name.second{left:12px;top:calc(50% + 8px)}+ }+ @media(prefers-reduced-motion:reduce){.stage[data-hb-mode="inside"] .c-flash,.stage[data-hb-mode="inside"] .l-flash{animation:none!important}}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10"><script src="../../../assets/js/demo-page.js?v=10" defer></script>@@ -78,6 +135,11 @@<span class="hb-num" aria-hidden="true">1</span><div><h2 id="hb-watch-title">Click it</h2><p>Press Copy and watch the button confirm, then change back, or press Show me.</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="copy-wrap"><div class="copy-row">@@ -93,14 +155,29 @@<span class="labels"><span class="l-copy">Copy</span><span class="l-copied">Copied!</span></span></button></div>+ <div class="views" aria-hidden="true">+ <span class="cap c-icons" data-hb-caption>So do the clipboard and the tick</span>+ <span class="cap c-words" data-hb-caption>Copy and Copied! share this spot</span>+ <span class="cap c-flash" data-hb-caption>A green flash spreads and fades</span>+ <span class="cap c-swap" data-hb-caption>Swaps in about a quarter of a second, with a small bounce</span>+ <span class="tbar"><i class="t-track"></i><i class="t-bar"></i><i class="t-mark"></i></span>+ <span class="cap c-bar" data-hb-caption>Changes back after a second and a half</span>+ </div><p class="fail" id="fail" aria-hidden="true" hidden>Couldn't copy. Select the code and copy it yourself.</p></div><span class="sr-only" id="live" role="status" aria-live="polite"></span>+ <svg class="leads" aria-hidden="true"><line class="l-icons"/><line class="l-words"/><line class="l-flash"/><line class="l-swap"/><line class="l-bar"/></svg>+ <span class="cap cmp-name first" aria-hidden="true" data-hb-caption>No sign</span>+ <span class="cap cmp-name second" aria-hidden="true" data-hb-caption>Confirms</span></div><div class="hb-player"><button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</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, both icons and both words share one spot each, so the button never changes size; the bar shows how long the confirmation stays. Press Copy, or Show me.</p>+ <p data-hb-for="compare" hidden>No sign: the code is copied but nothing shows it; Confirms: a tick, a green flash and Copied tell you it worked. Press Copy, or Show me.</p>+ </div></section><section class="hb-step hb-try" aria-labelledby="hb-try-title">@@ -192,7 +269,10 @@// Everything on the button that changes with .copied, so a run can start from rest without animatingconst moving=[btn,...btn.querySelectorAll('.ic svg,.ic .chk path,.labels span')];const PULSE=520; // length of the pulse ring in ms+ const NAME='Copy invite code',DONE='Copied: invite code'; // the button's name, which holds the visible word while Copied! showslet timer=0,failTimer=0,revert=1500;+ // How long Copied! stays: the pulse counts as movement (tripled in Slow motion) and the rest of the wait as a hold+ const stays=()=>PULSE*(slowTog.checked?3:1)+(revert-PULSE);function legacyCopy(text){return new Promise((res,rej)=>{@@ -216,37 +296,44 @@return viaApi.catch(()=>legacyCopy(text));}- // A real copy and Show me both show the confirmation here, and the button changes back on its own timer. The timer counts the- // pulse as movement (tripled while Slow motion is on) and the rest of the wait as a hold, so the button never changes back- // before a slowed pulse has ended.+ // A real copy and Show me both show the confirmation here; the button changes back on its own timer (stays() long).function showCopied(){btn.classList.add('copied');+ btn.setAttribute('aria-label',DONE);hideFail();clearTimeout(timer);- timer=setTimeout(()=>{btn.classList.remove('copied');live.textContent=''},PULSE*(slowTog.checked?3:1)+(revert-PULSE));+ lift();+ runStart=performance.now();runTotal=stays();+ timer=setTimeout(()=>{btn.classList.remove('copied');btn.setAttribute('aria-label',NAME);live.textContent='';settle()},runTotal);+ draw();}// The copy did not work: say so, and never leave "Copied!" showing (a Show me run may have put it there).function showFail(){clearTimeout(timer);btn.classList.remove('copied');+ btn.setAttribute('aria-label',NAME);live.textContent=fail.textContent;fail.hidden=false;const range=document.createRange();range.selectNodeContents(code);const picked=getSelection();picked.removeAllRanges();picked.addRange(range);clearTimeout(failTimer);failTimer=setTimeout(hideFail,5000);+ lift();settle();}function hideFail(){clearTimeout(failTimer);fail.hidden=true;if(live.textContent===fail.textContent)live.textContent='';+ settle();}btn.addEventListener('click',()=>{+ copying=true;copyText(code.innerText).then(()=>{+ copying=false;live.textContent='Copied to clipboard';showCopied();- },showFail);+ },()=>{copying=false;showFail()});});// Show me plays the confirmation without copying: writing to the clipboard would replace what the visitor copied, and@@ -260,6 +347,7 @@live.textContent='';moving.forEach(el=>el.style.setProperty('transition','none','important'));btn.classList.remove('copied');+ btn.setAttribute('aria-label',NAME);void btn.offsetWidth;moving.forEach(el=>el.style.removeProperty('transition'));}@@ -269,15 +357,120 @@});function choices(id,pick){- const buttons=document.querySelectorAll('#'+id+' button');- buttons.forEach(b=>b.addEventListener('click',()=>{+ const buttons=[...document.querySelectorAll('#'+id+' button')];+ buttons.forEach((b,i)=>b.addEventListener('click',()=>{buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});- pick(b.dataset);+ pick(b.dataset,i);+ draw();}));}- choices('speed-seg',d=>vars.setProperty('--dur',d.ms+'ms'));+ choices('speed-seg',(d,i)=>{vars.setProperty('--dur',d.ms+'ms');speedAt=i});choices('revert-seg',d=>{revert=+d.ms});- choices('feel-seg',d=>vars.setProperty('--ease',d.ease));+ choices('feel-seg',(d,i)=>{vars.setProperty('--ease',d.ease);feelAt=i});++ // See inside and Compare, drawn from the numbers above (--dur, --ease, stays()); they read the button and never change it.+ const $=s=>document.querySelector(s);+ const stage=$('.stage'),wrap=$('.copy-wrap'),row=$('.copy-row'),ic=btn.querySelector('.ic'),labs=btn.querySelector('.labels');+ const cap={},ln={};+ ['icons','words','flash','swap','bar'].forEach(k=>{cap[k]=$('.c-'+k);ln[k]=$('.l-'+k)});+ const tbar=$('.tbar'),bar=$('.t-bar'),mark=$('.t-mark');+ const motionOk=!matchMedia('(prefers-reduced-motion:reduce)').matches,phoneMQ=matchMedia('(max-width:600px)');+ const SCALE=4000,POSE_MS=120; // the bar's fixed scale (four seconds); how far into the swap Compare's copy is caught (the tick has begun to draw)+ const SWAP=[['almost half a second','more than a second'],['about a quarter of a second','almost a second'],['about a sixth of a second','about half a second']];+ const FEEL=['with a small bounce','slowing to a stop','at one steady speed'];+ const HALVES=['','half a second','a second','a second and a half','two seconds','two and a half seconds','three seconds','three and a half seconds','four seconds'];+ let mode='normal',speedAt=1,feelAt=0,copying=false,runStart=0,runTotal=0,markRaf=0,backTimer=0;+ if(!motionOk)$('[data-hb-for="compare"]').textContent='No sign: the code is copied but nothing shows it; Confirms: a tick and Copied tell you it worked. Press Copy, or Show me.';++ // Compare's copies, made once: the first half (a button that is only a picture) and the button caught in its swap+ const one=document.createElement('div'),row1=row.cloneNode(true),still=document.createElement('span');+ one.className='cmp-one';one.inert=true;one.setAttribute('aria-hidden','true');+ row1.querySelectorAll('[id]').forEach(e=>e.removeAttribute('id'));+ still.className=btn.className;still.innerHTML=btn.innerHTML;row1.querySelector('.copy-btn').replaceWith(still);+ one.append(row1);wrap.before(one);+ const pose=document.createElement('span');+ pose.className=btn.className+' pose';pose.innerHTML=btn.innerHTML;pose.inert=true;pose.hidden=true;pose.setAttribute('aria-hidden','true');+ row.after(pose);++ const say=(el,t)=>{if(el.textContent!==t)el.textContent=t};+ const total=()=>runStart?runTotal:stays(); // the run on show, else what the settings give+ function draw(){+ if(mode==='inside')drawInside();+ else if(mode==='compare'){if(canPose())drawPose();else pose.hidden=true}+ else pose.hidden=true;+ }+ document.addEventListener('hb:mode',e=>{mode=e.detail.mode;draw()});+ addEventListener('resize',draw);+ slowTog.addEventListener('change',draw);+ if(document.fonts)document.fonts.ready.then(draw);++ // See inside. The button is twice as large (margins make room), its two spots outlined; the bar is a fixed four seconds long+ // with the part Copied! stays filled and a marker that runs along it by the clock from the timer's start (the same sum).+ function drawInside(){+ btn.style.setProperty('--mx',btn.offsetWidth/2+'px');btn.style.setProperty('--my',btn.offsetHeight/2+'px');+ const t=total();+ say(cap.swap,'Swaps in '+SWAP[speedAt][+slowTog.checked]+', '+FEEL[feelAt]);+ say(cap.bar,'Changes back after '+(t%500?'about ':'')+HALVES[Math.round(t/500)]);+ bar.style.transform=`scaleX(${t/SCALE})`;+ place(t/SCALE);+ drawMark();+ }+ function drawMark(){+ const p=runStart&&motionOk?Math.min(1,(performance.now()-runStart)/runTotal):0;+ mark.style.transform=`translateX(${p*total()/SCALE*tbar.clientWidth}px)`;+ cancelAnimationFrame(markRaf);+ if(runStart&&motionOk&&p<1&&mode==='inside')markRaf=requestAnimationFrame(drawMark);+ }+ // Words go beside the button on a wide stage, over and under it on a phone's; a short line joins each to its part.+ function place(f){+ const s=stage.getBoundingClientRect(),ox=s.left+stage.clientLeft,oy=s.top+stage.clientTop,W=stage.clientWidth,ph=phoneMQ.matches;+ const R=el=>{const r=el.getBoundingClientRect();return{l:r.left-ox,t:r.top-oy,r:r.right-ox,b:r.bottom-oy,x:(r.left+r.right)/2-ox,y:(r.top+r.bottom)/2-oy}};+ const seg=(k,a,b,c,d)=>{const e=ln[k];e.setAttribute('x1',a);e.setAttribute('y1',b);e.setAttribute('x2',c);e.setAttribute('y2',d)};+ const mid=(v,a,b)=>Math.min(Math.max(v,a),b);+ // a label centered on x, kept inside the box+ const over=(el,x)=>{el.style.translate='';const r=R(el),nx=mid(x-(r.r-r.l)/2,6,W-6-(r.r-r.l));el.style.translate=(nx-r.l)+'px';return R(el)};+ const grow=o=>({l:o.l-6,t:o.t-6,r:o.r+6,b:o.b+6,x:o.x,y:o.y}); // a spot's dashed line stands 3px out, twice as large+ const B=R(btn),Io=grow(R(ic)),Wo=grow(R(labs)),T=R(tbar);+ let L;+ if(ph){+ L=over(cap.icons,Io.x);seg('icons',mid(Io.x,L.l+4,L.r-4),L.b,mid(Io.x,L.l+4,L.r-4),Io.t);+ L=over(cap.words,Wo.x);seg('words',mid(Wo.x,L.l+4,L.r-4),L.t,mid(Wo.x,L.l+4,L.r-4),Wo.b);+ }else{+ L=R(cap.icons);seg('icons',L.r,L.y,Io.l,Io.y);+ L=R(cap.words);seg('words',L.l,L.y,Wo.r,Wo.y);+ L=R(cap.flash);seg('flash',L.l,L.b,B.r-26,B.t+22);+ }+ const sw=fail.hidden&&!ph;L=R(cap.swap);cap.swap.style.visibility=fail.hidden?'':'hidden';+ ln.swap.style.display=sw?'':'none';seg('swap',mid(B.x,L.l+8,L.r-8),L.t,mid(B.x,L.l+8,L.r-8),B.b);+ const end=T.l+f*(T.r-T.l);L=over(cap.bar,end);seg('bar',mid(end,L.l+4,L.r-4),T.t+9,mid(end,L.l+4,L.r-4),L.t);+ }++ // Compare. The real button is never touched: a copy laid over it is caught POSE_MS into the swap (its transitions and flash are+ // paused there, so Speed and Feel set how far along it is). A press on the real button, or Show me, lifts it before the real+ // one starts; it comes back once the real one is at rest and nothing is pending.+ const canPose=()=>mode==='compare'&&!btn.classList.contains('copied')&&!copying&&fail.hidden;+ function drawPose(){+ pose.style.cssText=`left:${btn.offsetLeft}px;top:${btn.offsetTop}px;width:${btn.offsetWidth}px;height:${btn.offsetHeight}px`;+ pose.hidden=false;+ pose.classList.remove('copied');pose.classList.add('snap');void pose.offsetWidth;+ pose.classList.remove('snap');pose.classList.add('copied');void pose.offsetWidth;+ pose.getAnimations({subtree:true}).forEach(a=>{a.pause();a.currentTime=POSE_MS});+ }+ function lift(){+ if(mode!=='compare')return;+ pose.hidden=true;+ clearTimeout(backTimer);backTimer=setTimeout(draw,900); // a press that came to nothing+ }+ const early={capture:true,passive:true};+ stage.addEventListener('keydown',e=>{if((e.key==='Enter'||e.key===' ')&&e.target===btn)lift()},early);+ ['pointerdown','touchstart','click'].forEach(type=>stage.addEventListener(type,e=>{if(btn.contains(e.target))lift()},early));+ // The button is back at rest (or a refused copy has been shown): the bar's marker waits at its start and the views redraw+ function settle(){+ Promise.allSettled(btn.getAnimations({subtree:true}).map(a=>a.finished)).then(()=>{+ if(btn.classList.contains('copied'))return;+ runStart=0;draw();+ });+ }</script></body></html>