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: Hide-on-Scroll Header shows a track of the page with its top zone, the last move and the bar's dashed home (See inside) and an always-there bar above a bar that hides, posed half way at rest (Compare)
Claudee1019969e3d3
diff --git a/animations/01-scroll-based/hide-on-scroll-header/index.html b/animations/01-scroll-based/hide-on-scroll-header/index.htmlindex 7767097a..f1e93958 100644--- a/animations/01-scroll-based/hide-on-scroll-header/index.html+++ b/animations/01-scroll-based/hide-on-scroll-header/index.html@@ -52,6 +52,56 @@.pic{margin-top:26px;aspect-ratio:16/7;border-radius:10px;background:linear-gradient(135deg,#1f3a66,#3d2a5c 55%,#5a2d45)}.pic.two{background:linear-gradient(135deg,#123d3a,#1f3a66 60%,#2b2f45)}.post .end{margin-top:30px;padding-top:18px;border-top:1px solid rgba(255,255,255,.09);color:#8a8a92}+ /* See inside and Compare draw in layers of the stage, outside the scroller, that ignore the pointer and the keyboard */+ .fx,.fx-in,.fx-cmp,.home{display:none;position:absolute;pointer-events:none}+ .fx,.fx-in,.fx-cmp{inset:0}+ .fx{z-index:6;overflow:hidden}+ .stage[data-hb-mode="inside"] .fx,.stage[data-hb-mode="compare"] .fx,.stage[data-hb-mode="inside"] .home,.stage[data-hb-mode="compare"] .fx-cmp{display:block}+ .stage[data-hb-mode="inside"] .fx-in{display:flex}+ .cap{max-width:100%;padding:3px 8px;border-radius:10px;overflow-wrap:anywhere;text-wrap:balance;background:rgba(11,11,13,.95);color:#f4f4f2;+ border:1px solid rgba(244,244,242,.6);font:600 13px/1.3 var(--hb-font)}+ /* See inside: a track for the whole scroll (its top zone shaded, a marker at the current place, an arrow for the last move+ that counted) and the bar's home drawn as a dashed outline under the bar. Only the text is dimmed. */+ .stage[data-hb-mode="inside"] .post{opacity:.4}+ .home{top:0;left:0;right:0;height:var(--bar-h);z-index:1;border:2px dashed rgba(244,244,242,.85);background:rgba(11,11,13,.7)}+ .fx-in{flex-direction:column;padding:calc(var(--bar-h) + 8px) 8px 8px}+ .mid{position:relative;flex:1;min-height:0}+ .lbls{position:absolute;left:0;right:58px;top:0;bottom:0}+ .cap-zone,.cap-mv{position:absolute;right:0}+ .cap-mv{top:50%;translate:0 -50%;display:flex;gap:6px;align-items:center;border-style:solid}+ .car,.ca{fill:none;stroke:var(--ui-accent);stroke-width:2.4;stroke-linecap:round;stroke-linejoin:round}+ .car{flex:none;width:14px;height:14px}+ .fx-in[data-dir="up"] .car,.fx-in[data-dir="up"] .mka{rotate:180deg}+ .fx-in[data-dir="none"] .cap-mv{border-style:dashed}+ .fx-in[data-dir="none"] .car{opacity:.55}+ .trk{position:absolute;right:0;top:0;bottom:0;width:10px;background:#20222b;border:1px solid rgba(244,244,242,.6)}+ .zone{position:absolute;left:0;right:0;top:0;background:var(--ui-accent)}+ .mk{position:absolute;left:-7px;right:-7px;top:0;height:0}+ .mk::before{content:"";position:absolute;left:0;right:0;top:-2px;height:4px;background:#fff;box-shadow:0 0 0 1.5px #0b0b0d}+ .mka{display:none;position:absolute;right:100%;margin-right:5px;top:-11px;width:22px;height:22px}+ .fx-in:not([data-dir="none"]) .mka{display:block}+ .cap-bot{align-self:flex-start;margin-top:8px}+ /* Compare: the page twice, stacked, each half the box's width and half its height. The top half is a copy that follows the+ real scroll under a bar that never moves. The real box keeps its full size and only sits half a box lower, its lower half+ cut off by the stage, so its top half fills the lower half of the stage. The real bar slides up under the top half. */+ .stage[data-hb-mode="compare"]{overflow:clip}+ .stage[data-hb-mode="compare"] .scroller{inset:auto;left:0;right:0;top:calc(50% + 1px);height:100%}+ .stage[data-hb-mode="compare"]>.topbar{top:calc(50% + 1px)}+ .stage[data-hb-mode="compare"] .scroller:focus-visible{outline:none}+ .stage[data-hb-mode="compare"] .scroller:focus-visible+.fx::after{content:"";position:absolute;left:0;right:0;top:calc(50% + 1px);bottom:0;outline:2px solid var(--hb-accent,var(--ui-accent));outline-offset:-3px}+ .h1{position:absolute;top:0;left:0;right:0;height:calc(50% - 1px);overflow:hidden;background:#0b0b0d}+ .cpage{position:absolute;left:0;right:0;top:0;padding-top:var(--bar-h);will-change:transform}+ .h1 .topbar{transition:none}+ .split{position:absolute;left:0;right:0;top:calc(50% - 1px);height:2px;background:#8a8a92}+ .row{position:absolute;left:8px;right:8px;display:flex;flex-wrap:wrap;gap:6px;align-items:flex-start}+ .r1{top:calc(var(--bar-h) + 6px)}+ .r2{top:calc(50% + 7px + var(--bar-h))}+ .cap-rules{position:absolute;left:8px;bottom:8px}+ /* the real bar at rest, posed half way through leaving (only in Compare, so the halves differ while the bar is showing) */+ .stage[data-hb-mode="compare"] .topbar.posed{transform:translateY(calc(-50% - 1px))}+ .stage[data-hb-mode="compare"].shrink .topbar.posed{transform:translateY(-9px)}+ .stage[data-hb-mode="compare"].shrink .topbar.posed .brand{transform:translateY(4.5px) scale(.91)}+ .stage[data-hb-mode="compare"].shrink .topbar.posed .nav,.stage[data-hb-mode="compare"].shrink .topbar.posed .menu{transform:translateY(4.5px) scale(.95)}@media(max-width:600px){:root{--bar-h:52px}.nav{display:none}.menu{display:flex}}/* Reduced motion: the bar fades out and back instead of sliding, and the slim strip appears without moving */@media(prefers-reduced-motion:reduce){@@ -59,6 +109,10 @@.brand,.nav,.menu{transition:none}.topbar.away{transform:none;opacity:0}.stage.shrink .topbar.away{transform:translateY(-18px);opacity:1}+ .stage[data-hb-mode="compare"] .topbar.posed{transform:none;opacity:.5}+ .stage[data-hb-mode="compare"].shrink .topbar.posed{transform:translateY(-18px);opacity:1}+ .stage[data-hb-mode="compare"].shrink .topbar.posed .brand{transform:translateY(9px) scale(.82)}+ .stage[data-hb-mode="compare"].shrink .topbar.posed .nav,.stage[data-hb-mode="compare"].shrink .topbar.posed .menu{transform:translateY(9px) scale(.9)}}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10">@@ -82,6 +136,11 @@<span class="hb-num" aria-hidden="true">1</span><div><h2 id="hb-watch-title">Scroll it</h2><p>Scroll down inside the box, then back up, 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" id="stage"><div class="topbar" id="topbar" aria-hidden="true"><span class="brand"><span class="logo"></span>Field Notes</span>@@ -108,12 +167,36 @@<p class="end">The end of the page. Scroll back up.</p></article></div>+ <div class="home" inert aria-hidden="true"></div>+ <div class="fx" inert aria-hidden="true">+ <div class="fx-in" data-dir="none">+ <div class="mid">+ <div class="lbls">+ <p class="cap cap-zone" data-hb-caption>Top zone: the bar always shows</p>+ <p class="cap cap-mv" data-hb-caption><svg class="car" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M6 13l6 6 6-6"/></svg><span>Example: a move down hides it</span></p>+ </div>+ <div class="trk"><i class="zone"></i><div class="mk"><svg class="mka" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="11" fill="#0b0b0d" stroke="rgba(244,244,242,.6)" stroke-width="1.5"/><path class="ca" d="M12 7v10M8 13l4 4 4-4"/></svg></div></div>+ </div>+ <p class="cap cap-bot" data-hb-caption>Moves of a few pixels do not count; it slides in about a third of a second</p>+ </div>+ <div class="fx-cmp">+ <div class="h1"><div class="cpage"></div></div>+ <i class="split"></i>+ <div class="row r1"><p class="cap" data-hb-caption>Always there</p></div>+ <div class="row r2"><p class="cap cap-n2" data-hb-caption>Hides on scroll</p><p class="cap cap-pose" data-hb-caption hidden>Half way hidden</p></div>+ <p class="cap cap-rules" data-hb-caption>Takes about a third of a second; back on any scroll up</p>+ </div>+ </div></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><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 autocomplete="off"><span>Slow motion</span></label></div>+ <div class="hb-mode-notes" aria-live="polite">+ <p data-hb-for="inside" hidden>Inside, the track is the page top to bottom: in its shaded top zone the bar always shows; the arrow is the last move that counted. Scroll down and up, or press Show me.</p>+ <p data-hb-for="compare" hidden>Always there keeps the bar, so less of the page shows; Hides on scroll slides it away as you scroll down, shown half way at rest. Scroll down and up, or press Show me.</p>+ </div></section><section class="hb-step hb-try" aria-labelledby="hb-try-title">@@ -192,12 +275,23 @@const JITTER=6,MOVE=1100,HOLD=500; // a move under JITTER px is ignored; a Show me glide takes MOVE ms each waylet ms=300,comeBack='any',maxY=0,nearTop=112,lastY=0,away=false,ticking=false;let runFrame=0,runTimers=[],runFrom=0;+let mode='normal',dir=null,trackH=0,posed=false,posedAt=0,poseTimer=0; // dir: the last move that counted ('down', 'up' or none yet)+const POSE=500; // Compare: how long the box rests before the bar is posed half way again++// See inside and Compare: the listener sits here, at the top level; what it draws is defined below+document.addEventListener('hb:mode',e=>{+ mode=e.detail.mode;+ scroller.setAttribute('aria-label',label());+ clearTimeout(poseTimer);setPose(false,true);+ words();measure();+ if(mode==='compare')running()?schedulePose():setPose(!away,true);+});// The visitor pressed, typed, scrolled or touched inside the stage: a Show me run under way stops where it is.document.addEventListener('hb:input',()=>{if(running())stopRun()});// Layout is read here, never in the scroll handler: at the start, after the site font loads and on resize.- function measure(){maxY=Math.max(0,scroller.scrollHeight-scroller.clientHeight);nearTop=bar.offsetHeight*2}+ function measure(){maxY=Math.max(0,scroller.scrollHeight-scroller.clientHeight);nearTop=bar.offsetHeight*2;fit()}function setAway(on){if(on===away)return;away=on;bar.classList.toggle('away',on)}// Once per frame: near the top the bar always shows; below that, a move down hides it and a move up brings it back@@ -206,21 +300,26 @@function update(){ticking=false;const y=Math.min(Math.max(scroller.scrollTop,0),maxY);+ place(y); // the marker and the copy move on every scroll, before the early returnsif(y<=nearTop){setAway(false);lastY=y;return}const dy=y-lastY;if(Math.abs(dy)<JITTER)return;if(dy>0)setAway(true);else if(comeBack==='any')setAway(false);+ dir=dy>0?'down':'up';words(); // only a move that is judged changes the arrowlastY=y;}- scroller.addEventListener('scroll',()=>{if(!ticking){ticking=true;requestAnimationFrame(update)}},{passive:true});+ scroller.addEventListener('scroll',()=>{+ if(mode==='compare'&&!(posed&&scroller.scrollTop===posedAt)){setPose(false);schedulePose()} // any real move lifts the pose+ if(!ticking){ticking=true;requestAnimationFrame(update)}+ },{passive:true});// Slow motion makes the slide and Show me's glides three times longer (not the hold between them)function slow(){return slowTog.checked?3:1}- function applySpeed(){vars.setProperty('--dur',ms*slow()+'ms')}+ function applySpeed(){vars.setProperty('--dur',ms*slow()+'ms');words()}slowTog.addEventListener('change',applySpeed);// The shared script switches Slow motion off without a change event when reduced motion turns on: read it again after it- motionMQ.addEventListener('change',()=>setTimeout(applySpeed,0));+ motionMQ.addEventListener('change',()=>setTimeout(applySpeed,0)); // applySpeed writes the views' words again// Show me: one run, every timer and frame of it kept so stopRun() ends it at oncefunction running(){return runFrame!==0||runTimers.length>0}@@ -243,6 +342,7 @@// began. Under reduced motion it jumps instead: down, halfway back (where only "On any scroll up" brings the bar back), home.document.getElementById('btn-demo').addEventListener('click',()=>{const again=running(); // pressed during a run: it begins again from the same place+ if(mode==='compare'){setPose(false);schedulePose()}stopRun();measure();const from=again?runFrom:scroller.scrollTop,span=Math.round(scroller.clientHeight*1.5);const far=from+span<=maxY?from+span:Math.max(0,from-span);@@ -259,9 +359,10 @@document.getElementById('btn-reset').addEventListener('click',()=>{stopRun();bar.classList.add('snap');- scroller.scrollTop=0;lastY=0;setAway(false);+ scroller.scrollTop=0;lastY=0;setAway(false);dir=null;words();place(0);void bar.offsetWidth;bar.classList.remove('snap');+ if(mode==='compare'){clearTimeout(poseTimer);setPose(false,true);setPose(true,true)}});function choices(id,pick){@@ -272,8 +373,75 @@}));}choices('speed-seg',d=>{ms=+d.ms;applySpeed()});- choices('back-seg',d=>{comeBack=d.back});- shrinkTog.addEventListener('change',()=>stage.classList.toggle('shrink',shrinkTog.checked));+ choices('back-seg',d=>{comeBack=d.back;words()});+ shrinkTog.addEventListener('change',()=>{stage.classList.toggle('shrink',shrinkTog.checked);words()});++ // ── See inside and Compare ──+ const fx=stage.querySelector('.fx'),fxIn=fx.querySelector('.fx-in'),trk=fx.querySelector('.trk'),zone=fx.querySelector('.zone'),mk=fx.querySelector('.mk');+ const q=s=>fx.querySelector(s),capMv=q('.cap-mv span'),capBot=q('.cap-bot'),capN2=q('.cap-n2'),capPose=q('.cap-pose'),capRules=q('.cap-rules');+ const cpage=q('.cpage'),notes={inside:document.querySelector('[data-hb-for="inside"]'),compare:document.querySelector('[data-hb-for="compare"]')};+ const NOTE0={inside:notes.inside.textContent,compare:notes.compare.textContent};+ const put=(el,t)=>{if(el.textContent!==t)el.textContent=t};+ // The top half is a copy of the page, made once: no ids to repeat, inert and hidden from screen readers with its layer+ cpage.append(scroller.querySelector('.post').cloneNode(true));+ const bcopy=bar.cloneNode(true);bcopy.removeAttribute('id');bcopy.removeAttribute('aria-hidden');q('.h1').append(bcopy);+ const NOTES={ // the markup holds the default words; these replace them for Shrinks and for reduced motion+ inside:{rm:"Shown without sliding, as your device asks: the bar fades. In the track's shaded top zone it always shows; the arrow is the last move that counted.",+ rmShrink:"Shown without sliding, as your device asks: the slim strip appears at once. In the track's shaded top zone the whole bar always shows; the arrow is the last move that counted."},+ compare:{shrink:"Always there keeps the bar, so less of the page shows; Shrinks on scroll leaves a slim strip on the way down, half way at rest. Scroll, or press Show me.",+ rm:"Shown still: Always there keeps the bar; Hides on scroll fades its bar out as you scroll down, shown half faded at rest.",+ rmShrink:"Shown still: Always there keeps the whole bar; Shrinks on scroll leaves only a slim strip once you scroll down, as shown here."}};+ const span=t=>t<=200?'a sixth of a second':t<=400?'a third of a second':t<=550?'half a second':t<=800?'two thirds of a second':t<=1200?'a second':'two seconds';+ function label(){+ return ['A page whose top bar slides away as you scroll down',+ 'A page whose top bar slides away as you scroll down, with a track of the page, its top zone and the last move',+ 'The page twice, one above the other: a bar that always stays, and a bar that '+(shrinkTog.checked?'shrinks to a strip':'slides away')+' as you scroll down'][['normal','inside','compare'].indexOf(mode)];+ }+ // Every word the views write comes from the page's own state: Speed and Slow motion, Comes back, Shrinks, reduced motion, the last move+ function words(){+ const rm=motionMQ.matches,sh=shrinkTog.checked,home=comeBack==='top',key=rm?(sh?'rmShrink':'rm'):sh?'shrink':'';+ const how=rm?(sh?'a strip appears at once':'it fades away'):sh?'it shrinks to a strip':'it slides away';+ const back=rm?(sh?'the whole bar returns at once':'it fades back'):sh?'it grows back':'it slides back';+ fxIn.dataset.dir=dir||'none';+ put(capMv,dir==='down'?'Moved down: '+how:dir==='up'?'Moved up: '+(home?(sh?'it stays a strip until the top':'it stays away until the top'):back)+ :'Example: a move down '+(rm?(sh?'leaves a strip':'fades it'):sh?'shrinks it':'hides it'));+ put(capBot,'Moves of a few pixels do not count; '+(rm&&sh?'the strip appears at once':(sh?'it shrinks':rm?'it fades':'it slides')+' in about '+span(ms*slow())));+ put(capN2,sh?'Shrinks on scroll':'Hides on scroll');+ put(capPose,rm?(sh?'Shrunk to a strip':'Half faded'):sh?'Half way shrunk':'Half way hidden');+ put(capRules,(rm&&sh?'At once':'Takes about '+span(ms*slow()))+'; back '+(home?'only near the top':'on any scroll up'));+ for(const m in notes)put(notes[m],(NOTES[m]&&NOTES[m][key])||NOTE0[m]);+ if(mode!=='normal')scroller.setAttribute('aria-label',label());+ }+ // The track is measured here (when the mode, the size or the text changes), never once per scroll frame+ function fit(){+ trackH=trk.clientHeight;+ zone.style.height=(maxY>0?Math.min(1,nearTop/maxY):1)*trackH+'px';+ place(Math.min(Math.max(scroller.scrollTop,0),maxY));+ }+ function place(y){+ if(mode==='inside')mk.style.transform='translateY('+(maxY>0?y/maxY*trackH:0)+'px)';+ else if(mode==='compare')cpage.style.transform='translate3d(0,'+-y+'px,0)';+ }+ if(window.ResizeObserver)new ResizeObserver(fit).observe(trk);+ // Compare's real bar is posed half way through leaving while the box rests with the bar showing; a scroll or Show me lifts the pose+ function setPose(on,instant){+ if(on===posed)return;+ posed=on;posedAt=scroller.scrollTop;+ if(instant)bar.classList.add('snap');+ bar.classList.toggle('posed',on);capPose.hidden=!on; // its label is there only while the bar is posed+ if(instant){void bar.offsetWidth;bar.classList.remove('snap')}+ }+ function schedulePose(){+ clearTimeout(poseTimer);+ if(mode==='compare')poseTimer=setTimeout(function again(){if(running())poseTimer=setTimeout(again,POSE);else setPose(!away)},POSE);+ }+ // A wheel turn over the top half (a copy) scrolls the real box, unless the box is at that end and the page should scroll+ stage.addEventListener('wheel',e=>{+ if(mode!=='compare'||scroller.contains(e.target))return;+ const dy=e.deltaMode===1?e.deltaY*16:e.deltaMode===2?e.deltaY*scroller.clientHeight:e.deltaY,y=scroller.scrollTop;+ if(dy<0&&y<=0||dy>0&&y>=maxY-1)return;+ e.preventDefault();scroller.scrollTop=y+dy;+ },{passive:false});window.addEventListener('resize',measure);if(document.fonts&&document.fonts.ready)document.fonts.ready.then(measure);