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: Cover Card to Fixed Header shows four bars for where along the scroll each part changes, with a line for where the box is (See inside) and a cover that scrolls away beside one that shrinks and stays (Compare)

Claude61b81f7e4c50
diff --git a/animations/01-scroll-based/cover-card-to-fixed-header/index.html b/animations/01-scroll-based/cover-card-to-fixed-header/index.html
index d59af7a7..5cfe9d83 100644
--- a/animations/01-scroll-based/cover-card-to-fixed-header/index.html
+++ b/animations/01-scroll-based/cover-card-to-fixed-header/index.html
@@ -57,6 +57,52 @@
.end-cue{position:absolute;left:0;right:0;bottom:0;margin:0;display:flex;align-items:center;justify-content:center;
font-size:13px;color:#8a8a92;letter-spacing:.1em;text-align:center;padding:0 24px}
+ /* See inside and Compare. Normal never shows any of this: the layer, the copy, the bars and the names are display none. */
+ .hud,.twin,.strip,.hname{display:none}
+ .rows,.half{display:contents}
+ .hud{position:sticky;top:0;height:0;z-index:30;pointer-events:none}
+ .stage[data-hb-mode="inside"] .hud,.stage[data-hb-mode="compare"] .hud{display:block}
+ /* See inside: a strip across the bottom of the box, from just under the tall cover's author line down. Its width stands for the
+ start of the scroll, as far as the longest shrink; each row is a label and a bar for where along it one part changes, the
+ lit part of a bar is what the scroll has passed, and the line is where the box is. */
+ .stage[data-hb-mode="inside"] .strip{display:grid}
+ .strip{position:absolute;left:0;right:0;top:calc(85cqh - 16px);height:calc(15cqh + 16px);grid-template-rows:repeat(4,minmax(0,1fr));
+ padding:0 8px;background:rgba(5,8,10,.94);border-top:1px solid rgba(244,244,242,.28);font-size:clamp(12px,3.6cqh,14px);--lw:7.8em}
+ .srow{display:grid;grid-template-columns:var(--lw) minmax(0,1fr);gap:6px;align-items:center;min-height:0}
+ .slbl{font:500 1em/1 var(--hb-font);color:#f4f4f2;white-space:nowrap}
+ .strack{position:relative;height:100%}
+ .sbar{position:absolute;top:2px;bottom:2px;left:calc(var(--a,0) * 100%);width:calc((var(--b,1) - var(--a,0)) * 100%);
+ background:rgba(244,244,242,.22);border-radius:2px}
+ .sbar b{position:absolute;inset:0;background:var(--ui-accent);border-radius:2px;transform:scaleX(0);transform-origin:0 50%}
+ /* Snaps at halfway (and reduced motion): every part changes at one point, so each bar is a tick */
+ .strip.ticks .sbar{width:5px;margin-left:-2px}
+ .slane{position:absolute;top:2px;bottom:2px;left:calc(8px + var(--lw) + 6px);right:8px}
+ .sph{position:absolute;top:-1px;bottom:-1px;left:-1px;width:2px;border-radius:1px;background:#fff;will-change:transform}
+ /* Compare: the article twice in two columns of the same scroll. Left, the cover is an ordinary block that scrolls off with the
+ text; right, the page itself. The scene keeps the height Normal measures, so the scroll length never depends on the mode. */
+ .stage[data-hb-mode="compare"] .rows{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));height:var(--sh)}
+ .stage[data-hb-mode="compare"] .twin{display:block;overflow:clip;border-right:1px solid rgba(244,244,242,.3)}
+ .stage[data-hb-mode="compare"] .half{display:block;position:relative;overflow:clip}
+ .twin .cover{position:relative;top:auto;z-index:auto;will-change:auto}
+ .stage[data-hb-mode="compare"] .hname{display:block}
+ .hname{position:absolute;top:calc(100cqh - 30px);margin:0;padding:4px 9px;border-radius:999px;white-space:nowrap;
+ background:rgba(11,11,13,.9);border:1px solid rgba(244,244,242,.25);color:#f4f4f2;font:600 12px/1.2 var(--hb-font)}
+ .hn1{left:6px}.hn2{left:calc(50% + 6px)}
+ /* both halves are narrow, so the cover's text and the article are compacted the same way in each */
+ .stage[data-hb-mode="compare"] .cover-inner{padding:0 10px 12px}
+ .stage[data-hb-mode="compare"] .cover-meta{font-size:9px;margin-bottom:5px}
+ .stage[data-hb-mode="compare"] .cover-title{font-size:14px;letter-spacing:-.2px;margin-bottom:5px}
+ .stage[data-hb-mode="compare"] .cover-sub{font-size:11px;line-height:1.35;margin-bottom:8px}
+ .stage[data-hb-mode="compare"] .avatar{width:18px;height:18px}
+ .stage[data-hb-mode="compare"] .author-name{font-size:9px}
+ .stage[data-hb-mode="compare"] .header-chip{top:12px;right:8px}
+ .stage[data-hb-mode="compare"] .article-body{padding:12px 10px}
+ .stage[data-hb-mode="compare"] .article-body h3{font-size:10px;margin:14px 0 6px}
+ .stage[data-hb-mode="compare"] .article-body p{font-size:11px;line-height:1.55;margin-bottom:8px}
+ .stage[data-hb-mode="compare"] .article-body blockquote{font-size:12px;padding:5px 10px;margin:10px 0}
+ .stage[data-hb-mode="compare"] .end-cue{font-size:11px;padding:0 10px}
+ @media(max-width:600px){.stage[data-hb-mode="compare"] .chip-name{display:none}}
+
@media(max-width:600px){:root{--cover-min:64px}}
@media(prefers-reduced-motion:reduce){.cover,.cover-title,.cover-bg,.header-chip,.article-body{will-change:auto}}
</style>
@@ -81,9 +127,28 @@
<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="An article with a tall cover">
<div class="scene" id="scene">
+ <!-- Shown only in See inside (the bars) and Compare (the half names); one zero-height layer that stays at the top of the box -->
+ <div class="hud" aria-hidden="true">
+ <div class="strip" id="strip">
+ <div class="srow"><span class="slbl" data-hb-caption>Cover shrinks</span><span class="strack"><i class="sbar"><b></b></i></span></div>
+ <div class="srow"><span class="slbl" data-hb-caption>Subtitle fades</span><span class="strack"><i class="sbar"><b></b></i></span></div>
+ <div class="srow"><span class="slbl" data-hb-caption>Author fades</span><span class="strack"><i class="sbar"><b></b></i></span></div>
+ <div class="srow"><span class="slbl" data-hb-caption>Badge appears</span><span class="strack"><i class="sbar"><b></b></i></span></div>
+ <span class="slane"><u class="sph"></u></span>
+ </div>
+ <p class="hname hn1" data-hb-caption>Scrolls away</p>
+ <p class="hname hn2" data-hb-caption>Shrinks and stays</p>
+ </div>
+
+ <div class="rows" id="rows"><div class="half" id="half">
<div class="cover" id="cover">
<div class="cover-bg" id="cover-bg"></div>
<div class="cover-inner">
@@ -116,6 +181,7 @@
</article>
<p class="end-cue">The end · scroll back up and the cover opens again</p>
+ </div></div>
</div>
</div>
@@ -123,6 +189,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 bars show where along the scroll each part changes, and the line marks where you are: one scroll position sets them all. Scroll the box or press Play to watch.</p>
+ <p data-hb-for="compare" hidden>Scrolls away loses the title once the cover leaves; Shrinks and stays folds it into a slim header that keeps the title in view. Scroll the box or press Play to watch.</p>
+ </div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
@@ -194,11 +264,20 @@
const article = document.querySelector('.article-body'), endCue = document.querySelector('.end-cue');
const coverInner = document.querySelector('.cover-inner'), spacer = document.querySelector('.cover-spacer'); let CONTENT = 0;
const motionMQ = window.matchMedia('(prefers-reduced-motion: reduce)');
+ // Compare's left half: copies of the cover and the article, made once, with no ids. The copy's cover is an ordinary block, so
+ // it scrolls away with the text. It is inert and hidden from screen readers: the box's name says what each half shows.
+ const twin = document.createElement('div');
+ twin.className = 'twin'; twin.inert = true; twin.setAttribute('aria-hidden', 'true');
+ const twinCover = cover.cloneNode(true), twinArticle = article.cloneNode(true);
+ twinCover.querySelectorAll('.header-chip,.cover-rule').forEach(n => n.remove());
+ [twinCover, twinArticle].forEach(n => { n.removeAttribute('id'); n.querySelectorAll('[id]').forEach(x => x.removeAttribute('id')); twin.append(n); });
+ $('rows').prepend(twin);
+ let mode = 'normal';
// The full cover is 85% of the box and the slim one comes from a CSS variable that the phone rule
// overrides — so both are re-read in measure(), not frozen at startup, or a rotation across the
// 600px breakpoint would leave the collapsed header at the desktop size.
- let FULL = 0, MIN = 56;
+ let FULL = 0, MIN = 56, SH = 0;
// A wheel notch is ~100px — nearly a third of RANGE — and easeOutCubic turns that
// first notch into 68% of the collapse. Scroll therefore sets a *target* and the
@@ -224,9 +303,16 @@
function measure() {
MIN = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--cover-min')) || 56;
FULL = Math.round(stage.clientHeight * 0.85);
- scene.style.minHeight = Math.max(FULL + article.offsetHeight, stage.clientHeight + 440) + 'px';
+ // Compare lays the article out in a narrow column: the scene's size is always read from the full-width layout, so the scroll
+ // length never depends on the mode
+ const m = stage.getAttribute('data-hb-mode');
+ if (m === 'compare') stage.removeAttribute('data-hb-mode');
+ const articleH = article.offsetHeight;
+ if (m === 'compare') stage.setAttribute('data-hb-mode', m);
+ SH = Math.max(FULL + articleH, stage.clientHeight + 440);
+ scene.style.minHeight = SH + 'px'; scene.style.setProperty('--sh', SH + 'px');
endCue.style.height = (FULL - MIN) + 'px';
- cover.style.height = FULL + 'px'; CONTENT = FULL - spacer.getBoundingClientRect().height;
+ cover.style.height = twinCover.style.height = FULL + 'px'; CONTENT = FULL - spacer.getBoundingClientRect().height;
delete G.h; // the cover's slide is written again with the new FULL and CONTENT
}
@@ -234,6 +320,9 @@
const snaps = () => motionMQ.matches || snapTog.checked;
const curveOf = p => snaps() ? (p > 0.5 ? 1 : 0) : ease(p);
+ // In Compare the title ends larger: the halves are narrow
+ const endScale = () => mode === 'compare' ? .7 : 13 / 26;
+
// Takes the finished collapse value, so easing it bounds how far the header can
// travel in one frame — easing raw progress instead lets easeOutCubic (slope 3
// near zero) re-amplify a wheel notch back into a jump.
@@ -248,13 +337,65 @@
});
set('bgOp', q(lerp(1, .12, e), .01), v => coverBg.style.opacity = v);
set('blur', q(lerp(0, 6, e), BLUR_STEP), v => coverBg.style.filter = v > 0 ? `blur(${v}px)` : 'none');
- set('scale', q(lerp(1, 13 / 26, e), .002), v => coverTitle.style.transform = `scale(${v})`);
+ set('scale', q(lerp(1, endScale(), e), .002), v => coverTitle.style.transform = `scale(${v})`);
set('fade', q(1 - clamp(e * 3, 0, 1), .01), v => { coverMeta.style.opacity = v; coverSub.style.opacity = v; });
set('auth', q(1 - clamp(e * 2, 0, 1), .01), v => coverAuthor.style.opacity = v);
set('chip', q(clamp((e - .5) * 2, 0, 1), .01), v => headerChip.style.opacity = v);
set('rule', q(e, .01), v => coverRule.style.opacity = v);
}
+ // See inside: where along the scroll each part changes, as fractions of the longest shrink distance (easeOutCubic turned back
+ // into scroll, so the subtitle ends where the number reaches a third and the author where it reaches a half)
+ const SPAN = 440, strip = $('strip'), lane = strip.querySelector('.slane'), ph = strip.querySelector('.sph');
+ const bars = Array.from(strip.querySelectorAll('.sbar')), lits = bars.map(b => b.firstElementChild);
+ const unease = v => 1 - Math.pow(1 - v, 1 / 3);
+ let spans = [], laneW = 0;
+ function layoutStrip() {
+ const r = RANGE / SPAN, h = r / 2;
+ spans = snaps() ? [[h, h], [h, h], [h, h], [h, h]]
+ : [[0, r], [0, unease(1 / 3) * r], [0, unease(.5) * r], [unease(.5) * r, r]];
+ strip.classList.toggle('ticks', snaps());
+ bars.forEach((b, i) => { b.style.setProperty('--a', spans[i][0]); b.style.setProperty('--b', spans[i][1]); });
+ laneW = lane.clientWidth;
+ }
+ function drawStrip() {
+ if (mode !== 'inside') return;
+ const f = clamp(stage.scrollTop / SPAN, 0, 1);
+ ph.style.transform = `translateX(${f * laneW}px)`;
+ spans.forEach(([a, b], i) => { lits[i].style.transform = `scaleX(${b > a ? clamp((f - a) / (b - a), 0, 1) : (f > a ? 1 : 0)})`; });
+ }
+
+ // The notes: the markup holds the words for the default; Snaps at halfway and reduced motion get their own
+ const insideNote = document.querySelector('[data-hb-for="inside"]'), compareNote = document.querySelector('[data-hb-for="compare"]');
+ const NOTE = {
+ inside: insideNote.textContent,
+ insideSnap: 'Inside, Snaps at halfway is on, so every part changes at one point, half the shrink distance in; the line marks where you are. Scroll the box or press Play to watch.',
+ insideStill: 'Shown without animation: every part switches at one point, half the shrink distance in, and the line marks where you are as you scroll.',
+ compare: compareNote.textContent,
+ compareSnap: 'Scrolls away loses the title once the cover leaves; Shrinks and stays switches to a slim header halfway through the shrink distance. Scroll the box or press Play to watch.',
+ };
+ const LABELS = {
+ normal: stage.getAttribute('aria-label'),
+ inside: 'An article whose tall cover shrinks into a header, with bars for where along the scroll each part changes',
+ compare: 'The article twice: a cover that scrolls away, and one that shrinks into a header that stays',
+ };
+ function describe() {
+ insideNote.textContent = motionMQ.matches ? NOTE.insideStill : snapTog.checked ? NOTE.insideSnap : NOTE.inside;
+ compareNote.textContent = snaps() ? NOTE.compareSnap : NOTE.compare;
+ }
+
+ // The values render() keeps are cleared first, so the cover draws again with the mode's own sizes; the scroll position,
+ // Play and the focus are not touched
+ document.addEventListener('hb:mode', e => {
+ mode = e.detail.mode;
+ stage.setAttribute('aria-label', LABELS[mode]);
+ for (const k in G) delete G[k];
+ measure();
+ render(current);
+ describe(); layoutStrip(); drawStrip();
+ schedule();
+ });
+
function tick(now) {
const d = target - current;
if (Math.abs(d) < 0.0005) { current = target; render(current); raf = null; return; }
@@ -271,9 +412,10 @@
stage.addEventListener('scroll', () => {
target = curveOf(clamp(stage.scrollTop / RANGE, 0, 1));
schedule();
+ drawStrip();
}, { passive: true });
// Back to top, and Play starting again from the top, jump the box (the shared script moves it before the click reaches the document): the cover jumps with it
- document.addEventListener('click', e => { if (e.target.closest('#btn-top,#btn-scroll')) { target = current = curveOf(clamp(stage.scrollTop / RANGE, 0, 1)); render(current); } });
+ document.addEventListener('click', e => { if (e.target.closest('#btn-top,#btn-scroll')) { target = current = curveOf(clamp(stage.scrollTop / RANGE, 0, 1)); render(current); drawStrip(); } });
function choices(id, pick) {
const buttons = document.querySelectorAll('#' + id + ' button');
@@ -286,25 +428,34 @@
RANGE = +d.range;
target = curveOf(clamp(stage.scrollTop / RANGE, 0, 1));
schedule();
+ layoutStrip(); drawStrip();
});
snapTog.addEventListener('change', () => {
for (const k in G) delete G[k]; // the curve changed, so every value is stale
target = curveOf(clamp(stage.scrollTop / RANGE, 0, 1));
schedule();
+ describe(); layoutStrip(); drawStrip();
+ });
+ // Reduced motion turned on or off while the page is open: the cover, the bars and the notes follow
+ motionMQ.addEventListener('change', () => {
+ for (const k in G) delete G[k];
+ target = curveOf(clamp(stage.scrollTop / RANGE, 0, 1));
+ schedule(); describe(); layoutStrip(); drawStrip();
});
let rt;
addEventListener('resize', () => {
clearTimeout(rt);
- rt = setTimeout(() => { measure(); render(current); }, 150);
+ rt = setTimeout(() => { measure(); render(current); layoutStrip(); drawStrip(); }, 150);
});
measure();
target = current = curveOf(clamp(stage.scrollTop / RANGE, 0, 1));
render(current);
+ describe(); layoutStrip();
if (document.fonts && document.fonts.ready) {
- document.fonts.ready.then(() => { measure(); render(current); });
+ document.fonts.ready.then(() => { measure(); render(current); layoutStrip(); drawStrip(); });
}
</script>
</body>