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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stagger Reveal — Animation Handbook</title>
<meta name="description" content="Items in a group appear one after another as it scrolls in. Best for card grids.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/stagger-reveal/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
<link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Stagger Reveal — Animation Handbook">
<meta property="og:description" content="Items in a group appear one after another as it scrolls in. Best for card grids.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/stagger-reveal/">
<meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Stagger Reveal — Animation Handbook">
<meta name="twitter:description" content="Items in a group appear one after another as it scrolls in. Best for card grids.">
<meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Stagger Reveal", "name": "Stagger Reveal", "description": "Items in a group appear one after another as it scrolls in. Best for card grids.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/stagger-reveal/", "inLanguage": "en", "articleSection": "Scroll-Based", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Scroll-Based, Stagger Reveal"}</script>
<style>
:root{--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#6ea8ff;--ui-muted:#8a8a92}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;overflow-y:auto;scrollbar-width:none;background:#0b0b0d}
.stage::-webkit-scrollbar{display:none}
/* The last group ends with 80px of padding, more than the 20px an item sits low before it appears, so the hidden
items never add to the scroll length, which Play reads once when it starts. */
.section{padding:40px 24px;border-bottom:1px solid var(--ui-border)}
.section:last-of-type{padding-bottom:80px;border:none}
.sec-head{font-size:12px;text-transform:uppercase;letter-spacing:2px;color:var(--ui-accent);margin-bottom:20px}
/* Grid */
.grid{display:grid;grid-template-columns:repeat(4,1fr);gap:10px}
.grid-cell{background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:14px 10px;font-size:12px;text-align:center}
/* List */
.list-row{display:flex;align-items:center;gap:12px;padding:10px 0;border-bottom:1px solid var(--ui-border)}
.list-row:last-child{border:none}
.avatar{width:32px;height:32px;border-radius:50%;flex-shrink:0;background:var(--ui-accent)}
.list-label{flex:1;font-size:13px}
.list-meta{font-size:12px;color:var(--ui-muted)}
/* Chips */
.chips{display:flex;flex-wrap:wrap;gap:8px}
.chip{background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:20px;padding:6px 14px;font-size:12px}
/* Stagger states */
.item{opacity:0;transform:translateY(20px);transition:opacity .4s ease,transform .4s ease}
.item.in{opacity:1;transform:translateY(0)}
@media(max-width:600px){.grid{grid-template-columns:repeat(2,1fr)}}
@media(prefers-reduced-motion:reduce){.item{transition:none!important}}
/* See inside and Compare. Everything they add is hidden in Normal. */
.lay,.stairs,.copy,.cap{display:none}
.cap{padding:2px 8px;border-radius:999px;background:rgba(11,11,13,.92);border:1px solid rgba(255,255,255,.2);color:#f4f4f2;
font:600 12px/16px var(--hb-font);white-space:nowrap}
/* Layers fixed to the box: sticky and zero tall, with opaque bars, so nothing is added to the scroll */
.bar{position:absolute;left:0;right:0;height:var(--bar,30px);display:flex;align-items:center;justify-content:center;gap:8px;
padding:0 12px;background:#0b0b0d;pointer-events:none}
.bar-top{top:0;border-bottom:1px solid rgba(255,255,255,.12)}.bar-bot{bottom:0;border-top:1px solid rgba(255,255,255,.12)}
.stage.snap .item:not(.in),.grp.snap .item,.copy.snap .item{transition:none!important}
/* See inside: a tint by turn (--k is 1 for the first, 0 for the last), faint dashed items still waiting, a bar per item */
.stage[data-hb-mode="inside"],.stage[data-hb-mode="compare"]{overflow-anchor:none}
.stage[data-hb-mode="inside"] .lay-bot{display:block;position:sticky;bottom:0;height:0;z-index:6}
.stage[data-hb-mode="inside"] .cap-b1,.stage[data-hb-mode="inside"] .cap-b2{display:block}
.stage[data-hb-mode="inside"] .section{position:relative}
.stage[data-hb-mode="inside"] .item{position:relative;isolation:isolate}
.stage[data-hb-mode="inside"] .item::before{content:"";position:absolute;inset:0;z-index:-1;border-radius:inherit;pointer-events:none;
background:rgba(110,168,255,calc(.1 + .42 * var(--k,0)))}
.stage[data-hb-mode="inside"] .list-row{border-radius:6px;margin:0 -8px;padding-left:8px;padding-right:8px} /* the tint reaches past the text */
.stage[data-hb-mode="inside"] .list-meta{color:#d6d6de}
.stage[data-hb-mode="inside"] .item:not(.in){opacity:.25}
.stage[data-hb-mode="inside"] .item:not(.in),.stage[data-hb-mode="compare"] .item.ghost{outline:1.5px dashed rgba(244,244,242,.6);outline-offset:-1px}
.stage[data-hb-mode="inside"] .stairs{display:block;position:absolute;top:12px;right:24px;width:clamp(96px,32%,210px);height:48px;
border-left:1px solid rgba(255,255,255,.3);pointer-events:none}
.stairs i{position:absolute;display:block;border-radius:1px;background:var(--ui-accent)}
/* Compare: each group twice in one scroll, each section as tall as in Normal (measured in the script) */
.stage[data-hb-mode="compare"] .lay-top{display:block;position:sticky;top:0;height:0;z-index:6}
.stage[data-hb-mode="compare"] .lay-bot.posed{display:block;position:sticky;bottom:0;height:0;z-index:6}
.stage[data-hb-mode="compare"] .lay-bot.posed .cap-b3,.stage[data-hb-mode="compare"] .cap-l,.stage[data-hb-mode="compare"] .cap-r{display:block}
.stage[data-hb-mode="compare"] .bar-top{display:grid;grid-template-columns:1fr 1fr;column-gap:10px;padding:0 8px}
.stage[data-hb-mode="compare"] .bar-top::after{content:"";position:absolute;left:50%;top:0;bottom:0;width:1px;background:rgba(255,255,255,.28)}
.stage[data-hb-mode="compare"] .bar-top .cap{justify-self:center}
.stage[data-hb-mode="compare"] .section{position:relative;display:grid;grid-template-columns:1fr 1fr;column-gap:10px;align-content:start;
height:var(--h);overflow:clip;padding-left:8px;padding-right:8px}
.stage[data-hb-mode="compare"] .section::after{content:"";position:absolute;left:50%;top:0;bottom:0;width:1px;background:rgba(255,255,255,.28)}
.stage[data-hb-mode="compare"] .sec-head{grid-column:1/-1}
.stage[data-hb-mode="compare"] .grp,.stage[data-hb-mode="compare"] .copy{min-width:0;grid-row:2;grid-column:2}
.stage[data-hb-mode="compare"] .copy{display:block;grid-column:1}
.stage[data-hb-mode="compare"] .copy.grid{display:grid}
.stage[data-hb-mode="compare"] .copy.chips{display:flex}
.stage[data-hb-mode="compare"] .grid{gap:10px 6px}.stage[data-hb-mode="compare"] .grid-cell{padding:14px 2px}
.stage[data-hb-mode="compare"] .list-row{gap:4px;padding:14px 0} /* a smaller dot keeps the row 53px */
.stage[data-hb-mode="compare"] .avatar{width:24px;height:24px}.stage[data-hb-mode="compare"] .list-meta{display:none}
.stage[data-hb-mode="compare"] .list-label{min-width:0;font-size:12px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.stage[data-hb-mode="compare"] .chips{gap:4px}.stage[data-hb-mode="compare"] .chip{padding:4px 6px}
@media(max-width:600px){
.stage[data-hb-mode="inside"]{--bar:44px}
.stage[data-hb-mode="inside"] .bar-bot .cap{flex:1 1 0;min-width:0;white-space:normal;text-align:center;border-radius:14px}
.stage[data-hb-mode="compare"] .bar-top .cap{padding:2px 6px}
}
</style>
<link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
<script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="scroll" data-hb-autoplay>
<nav class="hb-bar" aria-label="Animation Handbook">
<a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
<span class="hb-pager"><a href="../reveal-on-scroll/" rel="prev" aria-label="Previous: Reveal on Scroll"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Reveal on Scroll</span></a><a href="../horizontal-scroll/" rel="next" aria-label="Next: Horizontal Scroll"><span><span class="hb-dir">Next: </span>Horizontal Scroll</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
<a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
<header class="hb-head">
<p class="hb-cat">01.13 · Scroll-Based</p>
<h1>Stagger Reveal</h1>
<p class="hb-lede">Items in a group appear one after another as it scrolls in. Best for card grids.</p>
</header>
<section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
<div class="hb-step-head">
<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. Back to top hides the groups again.</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="Three groups of items">
<span class="lay lay-top" aria-hidden="true"><span class="bar bar-top"><span class="cap cap-l" data-hb-caption>All at once</span><span class="cap cap-r" data-hb-caption>One after another</span></span></span>
<div class="section">
<div class="sec-head">01 · Grid</div>
<div class="grid grp" id="grid"></div>
</div>
<div class="section">
<div class="sec-head">02 · List</div>
<div class="grp" id="list"></div>
</div>
<div class="section">
<div class="sec-head">03 · Tags</div>
<div class="chips grp" id="chips"></div>
</div>
<span class="lay lay-bot" aria-hidden="true"><span class="bar bar-bot"><span class="cap cap-b1" data-hb-caption>Brighter: comes in sooner</span><span class="cap cap-b2" data-hb-caption>Each bar: one item fading in</span><span class="cap cap-b3" data-hb-caption>Just after the start</span></span></span>
</div>
<div class="hb-player">
<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, brighter items come in sooner, and each group's bars show every item starting a moment after the last, overlapping into one sweep. Press Back to top, then scroll or press Play to watch.</p>
<p data-hb-for="compare" hidden>All at once brings every item in together; One after another starts each a moment later, shown frozen just after the start. Press Back to top, then scroll or press Play to watch.</p>
</div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">2</span>
<div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then scroll again or press Play.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="delay-lbl">Delay between items</p>
<div class="seg" id="delay-seg" role="group" aria-labelledby="delay-lbl">
<button type="button" data-ms="30" aria-pressed="false">Short</button>
<button type="button" data-ms="50" class="on" aria-pressed="true">Medium</button>
<button type="button" data-ms="80" aria-pressed="false">Long</button>
</div>
<p class="hb-hint">Longer delays make the order easier to see.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="order-lbl">Order</p>
<div class="seg" id="order-seg" role="group" aria-labelledby="order-lbl">
<button type="button" data-dir="forward" class="on" aria-pressed="true">First to last</button>
<button type="button" data-dir="reverse" aria-pressed="false">Last to first</button>
<button type="button" data-dir="center" aria-pressed="false">From the middle</button>
<button type="button" data-dir="random" aria-pressed="false">Random</button>
</div>
<p class="hb-hint">Which item appears first.</p>
</div>
</div>
</section>
<section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">3</span>
<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
</div>
<div class="hb-card">
<p class="hb-prompt">Add a stagger reveal to [your grid, list or group of items]. When the group scrolls into view, reveal its items one after another instead of all at once: each item fades in and rises a little, starting a short delay after the one before, in the order the settings choose. Make each item's own animation longer than the delay between items, so they overlap into one sweep, and keep the whole cascade under about 600ms on long lists. If the visitor has reduced motion turned on, show the items without movement. Match the settings listed below.</p>
<div class="hb-chips-box" hidden>
<p class="hb-chips-title">Your settings</p>
<ul class="hb-chips"></ul>
</div>
<div class="hb-copy-row">
<button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
<p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
</div>
</div>
<div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
</section>
<section class="hb-about" aria-labelledby="hb-what-title">
<div class="hb-what">
<h2 id="hb-what-title">What it is</h2>
<div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
</div>
<div class="hb-fit">
<h2>Good for</h2>
<ul class="hb-tags hb-good"><li>Card grids</li><li>Search results</li><li>Menus</li><li>Galleries</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Long lists</li><li>Key information</li></ul>
</div>
</section>
<section class="hb-related" aria-labelledby="hb-related-title" hidden>
<h2 id="hb-related-title">Similar animations</h2>
<div class="hb-rel-list"></div>
</section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
const stage=document.getElementById('stage');
let staggerMs=50,direction='forward',observers=[],timers=[],mode='normal';
const FADE=400,POSE_WAIT=700,SCALE=1300,POSE_AT=75; // an item's fade; how long Compare waits before it holds a moment; the bars' time scale (the longest start plus one fade)
const reduced=()=>matchMedia('(prefers-reduced-motion:reduce)').matches;
// 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;
stage.classList.add('snap'); // the faint starts come and go in one step, not over the fade
if(mode==='compare')measure();
stage.setAttribute('aria-label',NAMES[mode]);
GROUPS.forEach(g=>mode==='compare'?pose(g):unpose(g));
words();poseLabel();
void stage.offsetWidth;stage.classList.remove('snap');
});
const GRID_ITEMS=['Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Eta','Theta','Iota','Kappa','Lambda','Mu'];
const LIST_ITEMS=[
{name:'Ada Lovelace',meta:'Algorithmics',color:'#79c0ff'},
{name:'Grace Hopper',meta:'Compilation',color:'#56d364'},
{name:'Alan Turing',meta:'Computation',color:'#d2a8ff'},
{name:'Claude Shannon',meta:'Information theory',color:'#ffa657'},
{name:'Tim Berners-Lee',meta:'WWW Protocol',color:'#58a6ff'},
{name:'Margaret Hamilton',meta:'Software engineering',color:'#f78166'},
];
const CHIPS=['Design','Travel','Music','Food','Books','Film','Sport','Science'];
function add(parent,cls,text){
const el=document.createElement('div');el.className=cls;
if(text)el.textContent=text;
parent.appendChild(el);return el;
}
function buildGrid(){
const g=document.getElementById('grid');g.textContent='';
return GRID_ITEMS.map(t=>add(g,'grid-cell item',t));
}
function buildList(){
const l=document.getElementById('list');l.textContent='';
return LIST_ITEMS.map(d=>{
const row=add(l,'list-row item');
add(row,'avatar').style.background=d.color;
add(row,'list-label',d.name);add(row,'list-meta',d.meta);
return row;
});
}
function buildChips(){
const c=document.getElementById('chips');c.textContent='';
return CHIPS.map(t=>add(c,'chip item',t));
}
function getOrder(n){
if(direction==='forward')return Array.from({length:n},(_,i)=>i);
if(direction==='reverse')return Array.from({length:n},(_,i)=>n-1-i);
if(direction==='center'){const c=Math.floor(n/2);return Array.from({length:n},(_,i)=>Math.abs(i-c));}
const a=Array.from({length:n},(_,i)=>i);for(let i=a.length-1;i>0;i--){const j=Math.floor(Math.random()*(i+1));[a[i],a[j]]=[a[j],a[i]];}return a;
}
// A timer that is remembered while it waits, so rebuild() can cancel the reveals that have not happened yet
function later(fn,wait){
const id=setTimeout(()=>{timers=timers.filter(t=>t!==id);fn()},wait);
timers.push(id);
}
// Each group has its own order, worked out once per build: the play, the tints, the bars and Compare's held moment all use it.
// order[i] is item i's turn; items with the same turn come in together. Its copy (Compare's left half) is made here too,
// without ids, inert and hidden from screen readers.
const GROUPS=[['grid',buildGrid],['list',buildList],['chips',buildChips]].map(([id,make])=>{
const box=document.getElementById(id),copy=document.createElement('div');
copy.className=box.className.replace('grp','copy');copy.inert=true;copy.setAttribute('aria-hidden','true');box.after(copy);
return {make,box,copy,sec:box.parentNode,items:[],order:[],maxTurn:0,done:false,posed:false,touched:[]};
});
const share=(g,i)=>g.maxTurn?1-g.order[i]/g.maxTurn:1; // 1 for the first turn, 0 for the last
function plan(g){
g.items=g.make();g.order=getOrder(g.items.length);g.maxTurn=Math.max(...g.order);
g.done=g.posed=false;g.touched=[];
g.items.forEach((el,i)=>el.style.setProperty('--k',share(g,i)));
g.copy.replaceChildren(...g.items.map(el=>el.cloneNode(true)));
drawBars(g);
}
// One bar per item, in turn order: it starts one Delay after the one above, is as long as a fade, on one time scale
function drawBars(g){
g.sec.querySelector('.stairs')?.remove();
const pitch=Math.min(4,48/g.items.length),box=document.createElement('div');
box.className='stairs';box.setAttribute('aria-hidden','true');
g.items.map((_,i)=>i).sort((a,b)=>g.order[a]-g.order[b]||a-b).forEach((i,row)=>{
const bar=document.createElement('i');
bar.style.cssText=`top:${row*pitch}px;height:${pitch-1}px;left:${g.order[i]*staggerMs/SCALE*100}%;width:${FADE/SCALE*100}%;opacity:${.3+.7*share(g,i)}`;
box.append(bar);
});
g.sec.append(box);
}
function staggerIn(g){
g.items.forEach((el,i)=>later(()=>el.classList.add('in'),g.order[i]*staggerMs));
later(()=>g.copy.querySelectorAll('.item').forEach(el=>el.classList.add('in')),0); // Compare's left half: all at once
later(()=>{g.done=true;if(mode==='compare')later(()=>pose(g),POSE_WAIT)},g.maxTurn*staggerMs+(reduced()?0:FADE));
}
// Compare holds a played group POSE_AT ms after its start, both halves, fades off (no timer stopped): the left items all at the same
// point of their fade, the right ones by turn, and the ones not started yet as dashed ghosts. A fixed time, so Delay changes it.
const ease=x=>1-(1-x)*(1-x); // close to the page's "ease"; the pose is a still
function snapped(g,fn){
g.box.classList.add('snap');g.copy.classList.add('snap');
fn();void g.box.offsetWidth;
g.box.classList.remove('snap');g.copy.classList.remove('snap');
}
function pose(g){
if(mode!=='compare'||!g.done||g.posed)return;
const rm=reduced();
function hold(el,start){
g.touched.push(el);
const e=POSE_AT<=start?0:ease(Math.min(1,(POSE_AT-start)/FADE)); // 0 is a ghost; levels run from .25 up to 1
if(!e)el.classList.add('ghost');
el.style.opacity=.25+.75*e;if(!rm)el.style.transform=`translateY(${20*(1-e)}px)`; // still, no movement, under reduced motion
}
snapped(g,()=>{
g.copy.querySelectorAll('.item').forEach(el=>hold(el,0));
g.items.forEach((el,i)=>hold(el,g.order[i]*staggerMs));
});
g.posed=true;poseLabel();
}
// Leaving Compare puts back only what the pose held
function unpose(g){
if(!g.posed)return;
snapped(g,()=>g.touched.forEach(el=>{el.style.opacity=el.style.transform='';el.classList.remove('ghost')}));
g.posed=false;g.touched=[];
}
const poseBar=stage.querySelector('.lay-bot');
function poseLabel(){
const top=stage.scrollTop,bottom=top+stage.clientHeight;
poseBar.classList.toggle('posed',mode==='compare'&&GROUPS.some(g=>g.posed&&g.sec.offsetTop<bottom&&g.sec.offsetTop+g.sec.offsetHeight>top));
}
stage.addEventListener('scroll',()=>{if(mode==='compare')poseLabel()},{passive:true});
// Compare: every section keeps its Normal height, so the scroll length that Play reads once never changes
function measure(){
const m=stage.getAttribute('data-hb-mode');
stage.style.overflowAnchor='none';
stage.setAttribute('data-hb-mode','normal');
GROUPS.forEach(g=>g.sec.style.setProperty('--h',getComputedStyle(g.sec).height));
stage.setAttribute('data-hb-mode',m);
stage.style.overflowAnchor='';
}
// The words of each mode: the markup holds the ones for motion; reduced motion swaps in its own
const noteIn=document.querySelector('[data-hb-for="inside"]'),noteCmp=document.querySelector('[data-hb-for="compare"]');
const capFade=document.querySelector('.cap-b2');
const MOTION={inside:noteIn.textContent,compare:noteCmp.textContent,fade:capFade.textContent};
const STILL={
inside:'Shown without movement: brighter items come in sooner, and each group\u2019s bars show every item starting a moment after the last. Press Back to top, then scroll to see them appear in turn.',
compare:'Shown still, just after the start: All at once has every item part way in, while One after another has only its first items started. Press Back to top, then scroll to see them appear.',
fade:'Each bar: one item coming in'};
function words(){
const w=reduced()?STILL:MOTION;
noteIn.textContent=w.inside;noteCmp.textContent=w.compare;capFade.textContent=w.fade;
}
const NAMES={normal:stage.getAttribute('aria-label'),
inside:'Three groups of items, each tinted by when it comes in, with bars for the timing',
compare:'Three groups of items twice: all at once, and one after another'};
matchMedia('(prefers-reduced-motion:reduce)').addEventListener('change',words);
GROUPS.forEach(plan);
// Each group plays once, when half of it is in the box or its bottom edge has come into the box. The observers watch the
// group's items, not the section around them, so the cascade starts while the items are on screen. A threshold every 5%
// makes them report on the way, so a jump straight to the end is not missed; a group less than half in view whose bottom
// edge is still below the box waits, hidden, until more of it scrolls in.
const STEPS=Array.from({length:21},(_,i)=>i/20);
function attachObservers(){
observers.forEach(o=>o.disconnect());observers=[];
GROUPS.forEach(g=>{
const o=new IntersectionObserver(entries=>{
const e=entries[entries.length-1];
if(e.isIntersecting&&(e.intersectionRatio>=.5||e.boundingClientRect.bottom<=e.rootBounds.bottom+1)){o.disconnect();staggerIn(g)}
},{root:stage,threshold:STEPS});
o.observe(g.box);observers.push(o);
});
}
attachObservers();
// Builds the three groups again, hidden, and watches them again: a group half in the box, or with its bottom edge in it,
// plays at once with the current settings, the others when they scroll in. The copies, the bars and any held moment are
// made again with them.
function rebuild(){
timers.forEach(clearTimeout);timers=[];
GROUPS.forEach(plan);
void stage.offsetWidth;
attachObservers();poseLabel();
}
// Back to top, and Play pressed with the box at its end, hide the groups again. The shared script jumps the box to the top
// when it handles the same click, after this listener; the new observers report on the next frame, after the jump, so the
// finished groups are never drawn at the top.
document.getElementById('btn-top').addEventListener('click',()=>rebuild());
document.getElementById('btn-scroll').addEventListener('click',()=>{
if(stage.scrollTop>=stage.scrollHeight-stage.clientHeight-2)rebuild();
});
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);
}));
}
choices('delay-seg',d=>{staggerMs=+d.ms;rebuild()});
choices('order-seg',d=>{direction=d.dir;rebuild()});
const relayout=()=>{if(mode==='compare')measure();poseLabel()};
let rt;addEventListener('resize',()=>{clearTimeout(rt);rt=setTimeout(relayout,150)});
if(document.fonts&&document.fonts.ready)document.fonts.ready.then(relayout);
words();
</script>
</body>
</html>