Skip to content

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.

feat: Scroll-Triggered Animation shows the start and end lines, the held place and a bar for each piece's progress (See inside) and the four zones with nothing triggered beside the same zones with their triggers (Compare)

Claudecaf7719905f9
diff --git a/animations/01-scroll-based/scroll-trigger/index.html b/animations/01-scroll-based/scroll-trigger/index.html
index 12461d8f..798468d7 100644
--- a/animations/01-scroll-based/scroll-trigger/index.html
+++ b/animations/01-scroll-based/scroll-trigger/index.html
@@ -50,7 +50,8 @@
/* Zone 3 — two boxes tall so the card has a span to stay pinned across,
and anchored to the zone top so the pin engages as the zone arrives. */
.z3{min-height:200cqh;justify-content:flex-start}
- .pin-card{position:sticky;top:calc(50cqh - 70px);width:100%;max-width:340px;margin:0 auto;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:20px}
+ .stage{--hold:calc(50cqh - 70px)}
+ .pin-card{position:sticky;top:var(--hold);width:100%;max-width:340px;margin:0 auto;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:20px}
.pin-step{font-size:12px;color:var(--ui-accent);margin-bottom:6px}
.pin-title{font-size:16px;font-weight:bold}
.pin-body{font-size:14px;color:var(--ui-muted);margin-top:8px;line-height:1.6}
@@ -58,6 +59,61 @@
.dots{display:flex;gap:14px;justify-content:center;margin-top:20px}
.dot{width:10px;height:10px;border-radius:50%;background:var(--ui-border);transition:background .2s,transform .2s}
.dot.lit{background:#f85149;transform:scale(1.5)}
+ /* See inside and Compare draw in one zero-tall layer that sticks to the top of the box, so the zones keep their heights.
+ It ignores the pointer, so a wheel turn or a touch over it still scrolls the box. */
+ .fx,.fx-in,.fx-cmp,.cm-copy{display:none}
+ .fx{position:sticky;top:0;height:0;z-index:6;pointer-events:none}
+ .fx-box{position:absolute;left:0;top:0;width:100%;height:100cqh;overflow:hidden}
+ .fx-in,.fx-cmp{position:absolute;inset:0}
+ .stage[data-hb-mode="inside"] .fx,.stage[data-hb-mode="compare"] .fx,.stage[data-hb-mode="inside"] .fx-in,.stage[data-hb-mode="compare"] .fx-cmp{display:block}
+ /* One zone's picture at a time: the zone under the middle of the box (data-z) shows its own parts */
+ .fx-in>*{display:none;position:absolute}
+ .fx-in[data-z="1"] .on1,.fx-in[data-z="2"] .on2,.fx-in[data-z="3"] .on3,.fx-in[data-z="4"] .on4,.fx-in>.say{display:block}
+ .cap{padding:2px 7px;border-radius:999px;white-space:nowrap;background:rgba(11,11,13,.95);color:#f4f4f2;
+ border:1px solid rgba(244,244,242,.3);font:600 12.5px/1.3 var(--hb-font)}
+ .say{left:8px;bottom:8px;max-width:calc(100% - 16px);white-space:normal;border-radius:12px}
+ .edge{left:0;right:0;height:3px;background:var(--ui-accent)}
+ .e-top{top:0}.e-bot{bottom:0}
+ .outl{left:0;top:0;border:2px dashed #f4f4f2;border-radius:6px;margin:-6px 0 0 -6px}
+ .band{left:0;right:0;top:calc(var(--e) * 100%);height:calc((var(--s) - var(--e)) * 100%);background:rgba(110,168,255,.14)}
+ .ln{left:0;right:0;height:0;border-top:2px dashed #f4f4f2;margin-top:-1px}
+ .l-start{top:calc(var(--s) * 100%)}.l-end{top:calc(var(--e) * 100%)}.l-hold{top:var(--hold)}
+ .c-start{left:8px;top:calc(var(--s) * 100% - 25px)}.c-end{left:8px;top:calc(var(--e) * 100% - 25px)}
+ .ring{left:0;top:0;width:22px;height:22px;margin:-11px 0 0 -11px;border:2px solid #fff;border-radius:50%;box-shadow:0 0 0 1px #000,inset 0 0 0 1px #000}
+ .fx-in[data-z="2"] .ring::after{content:"";position:absolute;inset:4px;border-radius:50%;background:#fff;box-shadow:0 0 0 1px #000}
+ /* The bars fill with the page's own follow value (the square) or count (the dots, the card's steps) */
+ .trk,.bar-dots>i,.bar-card>i{border:2px solid #f4f4f2;background:#0b0b0d;border-radius:4px;overflow:hidden}
+ .trk{left:0;top:0;width:10px;height:90px}
+ .trk>i{position:absolute;inset:0;background:var(--ui-accent);transform-origin:50% 100%;transform:scaleY(0)}
+ .bar-dots{left:0;top:0;width:106px;gap:4px}
+ .bar-dots>i{flex:1;height:12px}
+ .bar-card{left:0;top:0;width:14px;flex-direction:column;gap:4px}
+ .fx-in[data-z="4"] .bar-dots,.fx-in[data-z="3"] .bar-card{display:flex}
+ .bar-card>i{flex:1}
+ .bar-dots>.lit,.bar-card>.lit{background:var(--ui-accent)}
+ /* Compare: the zones twice, in two columns of the same scroll. The copy on the left has nothing triggered; the real zones
+ sit on the right. Each zone keeps its Normal height (--h, measured) and the words are set smaller to fit the halves. */
+ .stage[data-hb-mode="compare"]{overflow-anchor:none}
+ .stage[data-hb-mode="compare"] .cm-copy{display:block;position:absolute;left:0;top:0;width:50%}
+ .stage[data-hb-mode="compare"]>.zone,.stage[data-hb-mode="compare"]>.run-out{margin-left:50%}
+ .stage[data-hb-mode="compare"] .zone{height:var(--h);min-height:0;padding:32px 10px 14px}
+ .stage[data-hb-mode="compare"] .run-out{height:var(--h);padding:0 10px;font-size:12px;letter-spacing:.04em}
+ .stage[data-hb-mode="compare"] .zone-tag{left:8px;max-width:calc(100% - 16px);font-size:10px;letter-spacing:.4px}
+ .stage[data-hb-mode="compare"] .reveal-title{font-size:15px;margin-bottom:6px}
+ .stage[data-hb-mode="compare"] .reveal-sub,.stage[data-hb-mode="compare"] .pin-body{font-size:12px;line-height:1.45}
+ .stage[data-hb-mode="compare"] .zone-cap{font-size:12px;line-height:1.4}
+ .stage[data-hb-mode="compare"] .swatch{width:64px;height:64px;margin:22px auto 16px}
+ .stage[data-hb-mode="compare"] .dots{gap:9px;margin-top:14px}
+ .stage[data-hb-mode="compare"] .pin-card{max-width:none;padding:12px}
+ .stage[data-hb-mode="compare"] .pin-step{font-size:11px}
+ .stage[data-hb-mode="compare"] .pin-title{font-size:14px}
+ .stage[data-hb-mode="compare"] .pin-body{margin-top:6px}
+ .cm-copy .reveal-content{opacity:1;transform:none;transition:none}
+ .cm-copy .pin-card{position:static}
+ .cm-top{left:0;right:0;top:0;height:34px;background:rgba(11,11,13,.96)}
+ .cm-div{left:50%;top:0;bottom:0;width:2px;margin-left:-1px;background:#f4f4f2}
+ .fx-cmp>*{position:absolute}
+ .cm-n1{top:7px;left:8px}.cm-n2{top:7px;left:calc(50% + 8px)}
</style>
<link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10">
<script src="../../../assets/js/demo-page.js?v=10" defer></script>
@@ -80,7 +136,29 @@
<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 and it scrolls for you.</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="Four scroll zones">
+ <div class="fx" aria-hidden="true"><div class="fx-box">
+ <div class="fx-in" data-z="1">
+ <i class="on1 edge e-top"></i><i class="on1 edge e-bot"></i><i class="on1 outl"></i>
+ <i class="on2 on4 band"></i><i class="on2 on4 ln l-start"></i><i class="on2 on4 ln l-end"></i>
+ <span class="on2 on4 cap c-start" data-hb-caption>Start line</span><span class="on2 on4 cap c-end" data-hb-caption>End line</span>
+ <i class="on2 on4 ring"></i>
+ <i class="on2 trk bar-sq"><i></i></i>
+ <i class="on4 bar-dots"><i></i><i></i><i></i><i></i><i></i></i>
+ <i class="on3 ln l-hold"></i>
+ <i class="on3 bar-card"><i></i><i></i><i></i></i>
+ <span class="cap say" data-hb-caption></span>
+ </div>
+ <div class="fx-cmp">
+ <i class="cm-top"></i><i class="cm-div"></i>
+ <span class="cap cm-n1" data-hb-caption>No triggers</span><span class="cap cm-n2" data-hb-caption>With triggers</span>
+ </div>
+ </div></div>
<div class="zone z1" id="zone1">
<span class="zone-tag">Fades in</span>
<div class="reveal-content" id="reveal">
@@ -118,6 +196,10 @@
<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, the square and dots play while their middle crosses from the start line to the end line, and the card is held while its zone passes. Scroll the box or press Play to watch.</p>
+ <p data-hb-for="compare" hidden>In No triggers the four zones just scroll by; in With triggers the text fades in, the square turns, the card holds and the dots light. Scroll the box or press Play to watch.</p>
+ </div>
</section>
<section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
@@ -168,7 +250,33 @@
['Step 1 of 3', 'Held in place', 'The card stops here while the page keeps scrolling.'],
['Step 2 of 3', 'Changing', 'Its text changes as you scroll through the zone.'],
['Step 3 of 3', 'Let go', 'Scroll on and the card moves away with the page.']];
- let ticking = false;
+ let ticking = false, mode = 'normal';
+ const LABELS = {
+ normal: 'Four scroll zones',
+ inside: 'Four scroll zones with the trigger points of the zone in view marked',
+ compare: 'The four zones twice: with nothing triggered, and with their triggers'
+ };
+ // Compare's left half: a copy of the four zones with nothing triggered (the text is simply there, the square never turns, the
+ // card is not held, the dots stay dark). Made once, no ids, inert and hidden from screen readers; shown only in Compare.
+ const runOut = stage.querySelector('.run-out');
+ const copy = document.createElement('div');
+ copy.className = 'cm-copy'; copy.inert = true; copy.setAttribute('aria-hidden', 'true');
+ const copies = [...zones, runOut].map(z => {
+ const c = z.cloneNode(true);
+ c.removeAttribute('id'); c.querySelectorAll('[id]').forEach(n => n.removeAttribute('id'));
+ copy.append(c); return c;
+ });
+ stage.querySelector('.fx').after(copy);
+ // Under reduced motion the text appears instead of fading in, and Compare's note says so
+ const motionMQ = matchMedia('(prefers-reduced-motion: reduce)');
+ const compareNote = document.querySelector('[data-hb-for="compare"]'), FADES = compareNote.textContent;
+ const describe = () => { compareNote.textContent = motionMQ.matches ? FADES.replace('fades in', 'appears') : FADES; };
+ motionMQ.addEventListener('change', describe); describe();
+ document.addEventListener('hb:mode', e => {
+ mode = e.detail.mode;
+ stage.setAttribute('aria-label', LABELS[mode] || LABELS.normal);
+ remeasure();
+ });
const clamp = (v, a, b) => Math.min(Math.max(v, a), b);
const lerp = (a, b, t) => a + (b - a) * t;
const G = {}, set = (k, v, fn) => { if (G[k] !== v) { G[k] = v; fn(v); } };
@@ -178,6 +286,7 @@
its layout box (offsetTop, offsetHeight), which a transform does not change: the square is turned and scaled,
and the text sits 30px low until it has faded in. */
let ZT = [], ZH = [], VH = 1, TEXT = 0, TEXT_H = 0, SQUARE = 0, ROW = 0;
+ let CX = [0, 0, 0, 0], CARD0 = 0, CARD_H = 0, HELD = 0, CARD_END = 0, BAR_X = 0;
function measure() {
// clientTop: the stage's own border sits above the first pixel of its content
const sTop = stage.getBoundingClientRect().top + stage.clientTop, sy = stage.scrollTop;
@@ -188,6 +297,14 @@
TEXT = top(reveal, 0); TEXT_H = reveal.offsetHeight;
SQUARE = top(swatch, 1) + swatch.offsetHeight / 2; // the middle of the square
ROW = top(dotRow, 3) + dotRow.offsetHeight / 2; // the middle of the row of dots
+ // See inside: where the pieces stand across the box, and the card's places while it is held
+ const mid = el => el.offsetLeft + el.offsetWidth / 2;
+ CX[1] = mid(swatch); CX[3] = mid(dotRow);
+ outl.style.cssText = `width:${reveal.offsetWidth + 12}px;height:${TEXT_H + 12}px`;
+ const card = pinStep.parentNode;
+ CARD0 = top(card, 2); CARD_H = card.offsetHeight; HELD = parseFloat(getComputedStyle(card).top) || 0;
+ CARD_END = ZT[2] + zones[2].clientTop + zones[2].clientHeight - parseFloat(getComputedStyle(zones[2]).paddingBottom);
+ BAR_X = card.offsetLeft + card.offsetWidth + 8;
}
// Where a stretch of the scroll content stands against the box: not reached yet, in the box, or already behind it
const spanState = (top, h) => {
@@ -226,16 +343,55 @@
// Zone 3 — pinned, three stages
set('p3', Math.min(2, Math.floor(pinScrub(2) * 3)), v => {
pinStep.textContent = PIN_STEPS[v][0]; pinTitle.textContent = PIN_STEPS[v][1]; pinBody.textContent = PIN_STEPS[v][2];
+ if (mode === 'inside') CARD_H = pinStep.parentNode.offsetHeight; // the three texts do not wrap alike
});
// Zone 4 — five dots light up one by one as the row rises
set('p4', Math.round(follow(ROW) * 5), v => dots.forEach((d, i) => d.classList.toggle('lit', i < v)));
+ drawFx();
ticking = false;
}
+
+ /* See inside: the trigger points of the zone under the middle of the box, from the same numbers as update(): the follow
+ window (START, END), the held place (HELD) and the cached places of the pieces. The layer is redrawn on every frame. */
+ const fx = stage.querySelector('.fx-in'), $ = q => fx.querySelector(q);
+ const outl = $('.outl'), ring = $('.ring'), barSq = $('.bar-sq'), barDots = $('.bar-dots'), barCard = $('.bar-card'), say = $('.say');
+ fx.style.setProperty('--s', START); fx.style.setProperty('--e', END);
+ const SAY = ['Text: shown while any of it is in the box', 'Square: turns while its middle crosses the lines',
+ 'Card: held while its zone passes, in three steps', 'Dots: one lights for each fifth of the way'];
+ const mv = (el, x, y) => { el.style.transform = `translate(${x}px,${y}px)`; };
+ function drawFx() {
+ if (mode !== 'inside') return;
+ const st = stage.scrollTop, z = ZT.reduce((n, t, i) => st + VH / 2 >= t ? i : n, 0);
+ set('z', z, v => { fx.dataset.z = v + 1; say.textContent = SAY[v]; });
+ if (z === 0) mv(outl, reveal.offsetLeft, TEXT - st);
+ else if (z === 1) {
+ mv(ring, CX[1], SQUARE - st); mv(barSq, CX[1] + 90, SQUARE - st - 45);
+ barSq.firstChild.style.transform = `scaleY(${G.p2})`;
+ } else if (z === 3) {
+ mv(ring, CX[3], ROW - st); mv(barDots, CX[3] - 53, ROW - st + 20);
+ [...barDots.children].forEach((d, i) => d.classList.toggle('lit', i < G.p4));
+ } else {
+ // the card sits where the page keeps it: held at HELD, but never below the end of its zone
+ const y = Math.min(Math.max(CARD0 - st, HELD), CARD_END - CARD_H - st);
+ mv(barCard, BAR_X, y); barCard.style.height = CARD_H + 'px';
+ [...barCard.children].forEach((d, i) => d.classList.toggle('lit', i <= G.p3));
+ }
+ }
stage.addEventListener('scroll', () => { if (!ticking) { requestAnimationFrame(update); ticking = true; } }, { passive: true });
- let rt; addEventListener('resize', () => { clearTimeout(rt); rt = setTimeout(() => { measure(); update(); }, 150); });
- measure(); update();
+ /* Compare keeps every zone at exactly its Normal height: measure them with Normal's rules, then put the mode back. The
+ cached places are measured again (in a half the text wraps) and every written value is drawn again, at once. */
+ function fit() {
+ const m = stage.getAttribute('data-hb-mode'), all = [...zones, runOut];
+ stage.setAttribute('data-hb-mode', 'normal');
+ const H = all.map(z => z.getBoundingClientRect().height);
+ if (m) stage.setAttribute('data-hb-mode', m); else stage.removeAttribute('data-hb-mode');
+ all.forEach((z, i) => { z.style.setProperty('--h', H[i] + 'px'); copies[i].style.setProperty('--h', H[i] + 'px'); });
+ }
+ function remeasure() { fit(); measure(); Object.keys(G).forEach(k => delete G[k]); update(); }
+ let rt; addEventListener('resize', () => { clearTimeout(rt); rt = setTimeout(remeasure, 150); });
+ remeasure();
// The zones' heights come from the box, but the text inside them depends on the site font
- if (document.fonts && document.fonts.ready) document.fonts.ready.then(() => { measure(); update(); });
+ if (document.fonts && document.fonts.ready) document.fonts.ready.then(remeasure);
</script>
</body>
</html>