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: Pull to Refresh shows a dashed line to pull past, an arrow for how far the list moves and a longer one for how far the finger must (See inside) and a list that ignores the finger beside one that follows it and spins (Compare)
matin8fdae4ca5f06
diff --git a/animations/04-micro-interactions/pull-to-refresh/index.html b/animations/04-micro-interactions/pull-to-refresh/index.htmlindex 32d0f41f..f913b304 100644--- a/animations/04-micro-interactions/pull-to-refresh/index.html+++ b/animations/04-micro-interactions/pull-to-refresh/index.html@@ -70,6 +70,32 @@/* The status line is read by screen readers only */.sr-only{position:absolute;width:1px;height:1px;margin:-1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}@media(prefers-reduced-motion:reduce){.scroller,.indicator{transition:none!important}.card.fresh{animation:none}}+ /* See inside and Compare, drawn over the feed from the page's own numbers. See inside: the line a pull must pass, an arrow for how far the+ list moves and a longer one for how far the finger must, a still spinner where the real one stands at the line, the finger and the list's+ top riding their arrows. Compare: a copy of the screen that ignores the finger (inert, no ids) beside the real one, a ghost fingertip in each. */+ .twin,.views,.views .spin,.in-g,.in-l,.cmp-l,.v-knob,.v-tip{display:none}+ .views{position:absolute;inset:0;pointer-events:none}+ .views svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible}+ .v-line{stroke:var(--ui-accent);stroke-width:2;stroke-dasharray:7 6}+ .v-edge,.v-list,.v-fing{fill:none;stroke:#0b0b0d;stroke-width:6;stroke-linecap:round;stroke-linejoin:round}+ .v-list,.v-fing{stroke:#f4f4f2;stroke-width:3}+ .v-list{stroke:var(--ui-accent)}+ .v-knob{fill:var(--ui-accent);stroke:#0b0b0d;stroke-width:2}+ .v-tip circle{fill:rgba(244,244,242,.22);stroke:#f4f4f2;stroke-width:2}+ .v-tip circle+circle{fill:#f4f4f2;stroke:none}+ .lbl{position:absolute;padding:3px 8px;border-radius:5px;background:#0b0b0d;color:#f4f4f2;font:600 13px/1.25 var(--hb-font);white-space:nowrap}+ .cmp-l{left:50%;bottom:8px;translate:-50% 0}+ .views .spin{position:absolute;left:14px;opacity:1;border-radius:50%;background:#0b0b0d;box-shadow:0 0 0 4px #0b0b0d}+ .twin .card.fresh{animation:none}+ .views .on{display:inline}+ .stage[data-hb-mode="inside"] .views,.stage[data-hb-mode="inside"] .views .spin,.stage[data-hb-mode="inside"] .in-l,.stage[data-hb-mode="compare"] .views,.stage[data-hb-mode="compare"] .cmp-l{display:block}+ .stage[data-hb-mode="inside"] .in-g{display:inline}+ .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,1fr)}+ .stage[data-hb-mode="compare"]::after{content:"";position:absolute;left:50%;top:0;bottom:0;width:1px;background:rgba(255,255,255,.2);pointer-events:none}+ .stage[data-hb-mode="compare"]>.twin{display:flex;flex-direction:column;grid-row:1/3;min-width:0}+ .stage[data-hb-mode="compare"]>.app-bar,.stage[data-hb-mode="compare"]>.feed{grid-column:2}+ .stage[data-hb-mode="compare"] .indicator{padding:0 10px}+ @media(max-width:600px){.lbl{font-size:12px}}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10"><script src="../../../assets/js/demo-page.js?v=10" defer></script>@@ -92,7 +118,23 @@<span class="hb-num" aria-hidden="true">1</span><div><h2 id="hb-watch-title">Drag it</h2><p>Drag the list down from the top and let go, 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">+ <!-- Compare's first half: the same screen, made once, that never moves its list -->+ <div class="twin" aria-hidden="true" inert>+ <div class="app-bar"><span class="app-title">Updates</span><span class="refresh-btn"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/></svg></span></div>+ <div class="feed">+ <div class="scroller"><div class="list"></div></div>+ <div class="views">+ <svg><g class="v-tip"><circle r="12"/><circle r="4"/></g></svg>+ <span class="lbl cmp-l" data-hb-caption>No pull</span>+ </div>+ </div>+ </div><div class="app-bar"><span class="app-title" id="feed-title">Updates</span><button class="refresh-btn" id="trigger" type="button" aria-label="Refresh"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/></svg></button>@@ -103,6 +145,18 @@<span class="hint" id="hint">Pull to refresh</span></div><div class="scroller" id="scroller" role="region" aria-labelledby="feed-title" tabindex="0"><div class="list" id="list"></div></div>+ <div class="views" id="views" aria-hidden="true">+ <svg>+ <g class="in-g"><line class="v-line" id="v-line"/><path class="v-edge" id="v-edge"/><path class="v-list" id="v-list"/><path class="v-fing" id="v-fing"/></g>+ <circle class="v-knob" id="v-knob" r="5"/><g class="v-tip" id="v-tip"><circle r="12"/><circle r="4"/></g>+ </svg>+ <div class="spin s-ring" id="v-spin"><span class="ring"></span><span class="dots"><i></i><i></i><i></i></span><span class="bars"><i></i><i></i><i></i><i></i></span></div>+ <span class="lbl in-l" id="l-line" data-hb-caption>Let go past this line to refresh</span>+ <span class="lbl in-l" id="l-list" data-hb-caption>The list: only to the line</span>+ <span class="lbl in-l" id="l-fing" data-hb-caption>Your finger: almost twice as far</span>+ <span class="lbl in-l" id="l-spin" data-hb-caption>Then it spins about a second</span>+ <span class="lbl cmp-l" data-hb-caption>Pull to refresh</span>+ </div></div><p class="sr-only" id="status" role="status" aria-live="polite"></p></div>@@ -110,6 +164,10 @@<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><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></div>+ <div class="hb-mode-notes" aria-live="polite">+ <p data-hb-for="inside" hidden>Inside, the dashed line is where letting go refreshes; the arrows show your finger moves farther than the list, which resists. Drag the list down, or press Show me.</p>+ <p data-hb-for="compare" hidden>No pull: the list ignores your finger and new items just pop in; Pull to refresh: it follows, resisting, and spins until new items arrive. Drag the list, or press Show me.</p>+ </div></section><section class="hb-step hb-try" aria-labelledby="hb-try-title">@@ -208,6 +266,9 @@let threshold=64,resistance=2,dur=1200;let startY=0,pid=null,pulling=false,pull=0,refreshing=false,refreshTimer=0,refreshFrom=0,n=0;let runTimers=[],runFrame=0,demoRefresh=false;+ // The mode on show, the finger's distance while it pulls (null while it does not), whether Compare's pose is on, and the timer that brings the pose back+ let mode='normal',fing=null,posed=false,poseTimer=0,geo=null;+ const feed=scroller.parentElement,twin=document.querySelector('.twin'),twList=twin.querySelector('.list'),twScroller=twin.querySelector('.scroller');const SEED=['Design review moved to 3pm','Your order has shipped','Ana replied to your comment','Weekly report is ready','Invoice from Northwind paid','Team notes posted'];function el(tag,cls,text){const e=document.createElement(tag);e.className=cls;if(text)e.textContent=text;return e}@@ -217,13 +278,18 @@c.append(el('span','dot'),meta);return c;}- function build(){listEl.replaceChildren(...SEED.map(t=>card(t,false)))}+ function build(){listEl.replaceChildren(...SEED.map(t=>card(t,false)));syncTwin()}+ // Compare's first list is a copy of the real one, made again whenever the real one changes+ function syncTwin(){twList.replaceChildren(...[...listEl.children].map(c=>c.cloneNode(true)));twScroller.scrollTop=scroller.scrollTop}+ // The copy scrolls with the real list; a scrolled list lifts the pose, which comes back once the list is at its top again+ scroller.addEventListener('scroll',()=>{twScroller.scrollTop=scroller.scrollTop;if(scroller.scrollTop<1)settle(900);else lift()});// Move both the indicator and the list by the same amount; optionally tween (never under reduced motion).function setY(y,animate){const t=(animate&&!reduceMq.matches)?'transform .34s cubic-bezier(.22,1,.36,1)':'none';scroller.style.transition=indicator.style.transition=t;scroller.style.transform=indicator.style.transform='translateY('+y+'px)';+ drawFinger();}// Rubber-band: the list follows the finger 1:1 at first and stiffens as the drag grows; Resistance sets how fast.function resist(dy){const max=threshold*(1+2.4/resistance);return max*dy/(dy+max)}@@ -240,6 +306,7 @@scroller.addEventListener('pointerdown',e=>{if(refreshing||pulling)return;if(e.pointerType==='mouse'&&e.button!==0)return;+ lift();pid=e.pointerId;startY=e.clientY;});scroller.addEventListener('pointermove',e=>{@@ -250,17 +317,17 @@if(dy>4&&scroller.scrollTop<=0){pulling=true;try{scroller.setPointerCapture(pid)}catch(_){}}else return;}- if(dy<=0){pull=0;setY(0,false);drawSpin();pulling=false;return}- pull=resist(dy);+ if(dy<=0){pull=0;fing=null;setY(0,false);drawSpin();pulling=false;return}+ pull=resist(dy);fing=dy;setY(pull,false);drawSpin();});// Letting go past the line refreshes; a shorter pull, or a gesture the browser cancelled, springs back.function letGo(cancelled){const was=pulling;pid=null;pulling=false;- if(!was||refreshing)return;+ if(!was||refreshing){settle(0);return}if(!cancelled&&pull>=threshold)startRefresh();- else{pull=0;setY(0,true);drawSpin()}+ else{pull=0;fing=null;setY(0,true);drawSpin();settle(800)}}scroller.addEventListener('pointerup',e=>{if(e.pointerId===pid)letGo(false)});scroller.addEventListener('pointercancel',e=>{if(e.pointerId===pid)letGo(true)});@@ -272,7 +339,7 @@function startRefresh(){if(refreshing)return;cancelAnimationFrame(runFrame);runFrame=0; // a Show me pull still being drawn hands over to the refresh- refreshing=true;pull=INDH;+ refreshing=true;pull=INDH;fing=null;setY(INDH,true);indicator.classList.add('ready');hint.textContent='Refreshing…';@@ -284,15 +351,16 @@function finishRefresh(){refreshTimer=0;n++;- listEl.insertBefore(card('New update #'+n+' arrived',true),listEl.firstChild);+ listEl.insertBefore(card('New update #'+n+' arrived',true),listEl.firstChild);syncTwin();scroller.scrollTop=0;spin.classList.remove('spinning');pull=0;setY(0,true);drawSpin();refreshing=false;// Show me's refresh says nothing aloud, because nothing was really updated: its new item shows for 1.5 s, whatever Refresh time became,// then the list is put back to its first six items- if(demoRefresh){demoRefresh=false;later(toRest,0,1500);return}+ if(demoRefresh){demoRefresh=false;later(()=>{toRest();restPose()},0,1500);return}statusEl.textContent='Updated — 1 new item added.';+ settle(1500);}// Show me: one run at a time. Every timer of the run goes through later(); stopRun() cancels the whole run.@@ -309,7 +377,7 @@// turned off, the state is set, a reflow makes it stick, and the transitions come back. A refresh under way ends here too.function toRest(){clearTimeout(refreshTimer);refreshTimer=0;- refreshing=false;pulling=false;pid=null;+ refreshing=false;pulling=false;pid=null;posed=false;fing=null;spin.classList.remove('spinning');n=0;statusEl.textContent='';if(listEl.children.length!==SEED.length)build();@@ -324,7 +392,7 @@const t0=performance.now();(function frame(now){const k=Math.min(1,Math.max(0,(now-t0)/ms));- pull=1.25*threshold*(1-Math.pow(1-k,3));+ pull=1.25*threshold*(1-Math.pow(1-k,3));fing=unresist(pull);setY(pull,false);drawSpin();runFrame=k<1?requestAnimationFrame(frame):0;})(t0);@@ -338,21 +406,104 @@pullDown(700);later(release,0,850);});- $('btn-reset').addEventListener('click',()=>{stopRun();toRest()});- $('trigger').addEventListener('click',()=>startRefresh()); // the keyboard and button way+ $('btn-reset').addEventListener('click',()=>{stopRun();toRest();restPose()});+ $('trigger').addEventListener('click',()=>{lift();startRefresh()}); // the keyboard and button way// The visitor pressed, typed, scrolled or touched inside the stage: the run stops. A pull still under way springs back and the// visitor's own drag takes over; a refresh that has started finishes as usual, and its new item stays.document.addEventListener('hb:input',()=>{if(!running())return;stopRun();- if(!refreshing){pull=0;setY(0,true);drawSpin()}+ if(!refreshing){pull=0;fing=null;setY(0,true);drawSpin()}+ settle(900);+ });++ // See inside and Compare. The finger's drag that moves the list by p is the inverse of resist().+ function unresist(p){const max=threshold*(1+2.4/resistance);return max*p/(max-p)}+ // Compare rests on a pose: the list pulled to the line, its spinner saying "Release to refresh", the finger far below it. A press on the+ // list, Show me, Reset or the Refresh button lifts it at once, with no glide; it comes back when nothing is pulled, running or pending.+ function pose(){posed=true;pull=threshold;fing=unresist(pull);setY(pull,false);drawSpin()}+ function lift(){+ clearTimeout(poseTimer);poseTimer=0;+ if(!posed)return;+ posed=false;pull=0;fing=null;setY(0,false);drawSpin();+ void scroller.offsetWidth;scroller.style.transition=indicator.style.transition='';+ }+ const idle=()=>!pulling&&pid===null&&!refreshing&&!running()&&!poseTimer&&scroller.scrollTop<1;+ function restPose(){if(mode==='compare'&&idle())pose()}+ function settle(ms){clearTimeout(poseTimer);poseTimer=mode==='compare'?setTimeout(()=>{poseTimer=0;restPose()},ms):0}++ const V={line:$('v-line'),edge:$('v-edge'),list:$('v-list'),fing:$('v-fing'),knob:$('v-knob'),tip:$('v-tip'),still:$('v-spin'),+ lLine:$('l-line'),lList:$('l-list'),lFing:$('l-fing'),lSpin:$('l-spin')};+ const TW={tip:twin.querySelector('.v-tip')};+ const at=(el,x,y)=>el.setAttribute('transform','translate('+x+' '+y+')');+ // The finger on its arrow and the list's top on the short one (See inside), or a ghost fingertip in each half+ // (Compare, kept clear of the half names; the real half shows none while the visitor's own finger pulls). Called by setY().+ function drawFinger(){+ if(!geo)return;+ const on=fing!==null,{H,y0,xL,xF,cx}=geo;+ if(mode==='inside'){+ V.tip.classList.toggle('on',on);V.knob.classList.toggle('on',on);+ if(on){at(V.tip,xF,Math.min(y0+fing,H-8));at(V.knob,xL,Math.min(y0+pull,H-8))}+ return;+ }+ [[V,!pulling],[TW,true]].forEach(([h,show])=>{+ h.tip.classList.toggle('on',on&&show);+ if(on&&show)at(h.tip,cx,Math.min(y0+fing,H-48));+ });+ }+ // Draws the mode on show from the page's own numbers (nothing in Normal): measured again on a mode press, a setting and a new size.+ function drawViews(){+ [V.tip,V.knob,TW.tip].forEach(e=>e.classList.remove('on'));+ if(mode==='normal'){geo=null;return}+ const W=feed.clientWidth,H=feed.clientHeight,sb=scroller.offsetWidth-scroller.clientWidth,y0=listEl.offsetTop,T=threshold,F=T*(1+resistance/2.4);+ const xL=W-sb-36,xF=W-sb-14;+ geo={H,y0,xL,xF,cx:(W-sb)/2};+ if(mode==='inside'){+ // The line is where the list's first card must get to; the short arrow is how far the list moves, the long one how far the finger must.+ const ly=y0+T,arrow=(x,y)=>'M'+x+' '+y0+'V'+y+'M'+(x-5)+' '+(y-8)+'L'+x+' '+y+'L'+(x+5)+' '+(y-8),say=(el,t)=>{if(el.textContent!==t)el.textContent=t};+ [['x1',0],['x2',W],['y1',ly],['y2',ly]].forEach(([k,v])=>V.line.setAttribute(k,v));+ V.list.setAttribute('d',arrow(xL,ly));V.fing.setAttribute('d',arrow(xF,y0+F));V.edge.setAttribute('d',arrow(xL,ly)+arrow(xF,y0+F));+ V.still.className=spin.className.replace(/ ?spinning/,'');V.still.style.top=(ly-55)+'px'; // where the real spinner stands with the list at the line+ say(V.lFing,'Your finger: '+['nearly half as far again','almost twice as far','more than twice as far'][resistance-1]);+ say(V.lSpin,'Then it spins '+(dur<1000?'under a second':dur<1600?'about a second':'for two seconds'));+ // Each label takes the first place near what it names that covers no other label, arrow, line or the spinner+ const taken=[{x:10,y:ly-59,r:44,b:ly-25},{x:0,y:ly-3,r:W,b:ly+3},{x:xL-8,y:y0-3,r:xL+8,b:ly+4},{x:xF-8,y:y0-3,r:xF+8,b:y0+F+4}];+ const hit=(a,b)=>a.x<b.r&&b.x<a.r&&a.y<b.b&&b.y<a.b;+ const put=(el,x,y)=>{+ const w=el.offsetWidth,h=el.offsetHeight;let best;+ for(const j of [0,1,-1,2,-2,3,-3,4,-4]){+ const px=Math.max(4,Math.min(W-w-4,x)),py=Math.max(4,Math.min(H-h-4,y+j*(h+4))),a={x:px,y:py,r:px+w,b:py+h},c=taken.filter(o=>hit(a,o)).length;+ if(!best||c<best.c)best={a,c};+ if(!c)break;+ }+ const a=best.a;el.style.left=a.x+'px';el.style.top=a.y+'px';taken.push({x:a.x-2,y:a.y-2,r:a.r+2,b:a.b+2});+ };+ put(V.lSpin,50,ly-42-V.lSpin.offsetHeight/2);+ put(V.lList,xL-10-V.lList.offsetWidth,ly-6-V.lList.offsetHeight);+ put(V.lLine,8,ly+7);+ put(V.lFing,xF-10-V.lFing.offsetWidth,y0+F-V.lFing.offsetHeight-4);+ }+ drawFinger();+ }+ // The mode switch: draws the mode at once (Compare's pose, or the pose lifted). The shared script then presses Show me (not under+ // reduced motion), which plays it. A pull or a refresh under way is left as it is.+ document.addEventListener('hb:mode',e=>{+ mode=e.detail.mode;+ twScroller.scrollTop=scroller.scrollTop;+ if(mode==='compare')restPose();else lift();+ drawViews();});+ addEventListener('resize',drawViews);+ const sizes=new ResizeObserver(drawViews);+ sizes.observe(feed);sizes.observe(scroller);function choices(id,pick){const buttons=document.querySelectorAll('#'+id+' button');buttons.forEach(b=>b.addEventListener('click',()=>{buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});pick(b.dataset);+ if(posed)pose();+ drawViews();}));}choices('th-seg',d=>{threshold=+d.px});