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.

fix: the chart version picks its layout by the box's size, so a phone held sideways gets a readable chart, and Back to top shows the first step at once

matin8afcd3a54f83
diff --git a/famous/versions/pudding-chart.html b/famous/versions/pudding-chart.html
index 2aa8f238..9cb80674 100644
--- a/famous/versions/pudding-chart.html
+++ b/famous/versions/pudding-chart.html
@@ -33,20 +33,27 @@ body::after{content:"";position:fixed;left:0;top:0;width:min(23em,40%);height:72
.ct span,.l,.ax{opacity:0;transition:opacity .45s ease}
[data-ch="1"] :is(.ax,.ct span:first-child),:is([data-k="1"],[data-k="2"]) .l.w,[data-k="3"] .l.v,[data-ch="2"] :is(.l.g,.ct span:last-child){opacity:1;transition-delay:.55s} /* labels come back after the dots have mostly arrived, and leave at once */
.tiny .ct{display:none}
-.snap *,.snap *::before,.snap *::after{transition:none!important}
+.snap *,.snap *::before,.snap *::after{transition:none!important} /* on the page itself, so a jump (Back to top) shows its step, cards included, at once */
.lite .d::before,.lite .d::after{box-shadow:none}
.steps{position:relative;z-index:2;display:flow-root;list-style:none;width:min(23em,40%);margin-top:calc(var(--H) * -1);padding:0 12px 0 clamp(14px,3vw,30px)}
-.card{position:relative;margin-bottom:calc(var(--H) * .46);padding:13px 15px 14px;border-radius:14px;background:#151722;border:1px solid #2b3047;transition:opacity .3s ease} /* opaque, so the chart never shows through a card */
-.card:nth-last-child(2){margin-bottom:0}
+.card{position:relative;padding:13px 15px 14px;border-radius:14px;background:#151722;border:1px solid #2b3047;transition:opacity .3s ease} /* opaque, so the chart never shows through a card */
+.card+.card{margin-top:calc(var(--H) * .46)}
.end{height:var(--endh,0px)} /* room after the last card, so it ends just above the middle (on phones, so it can stay put at the middle) */
.card b,.card p{opacity:.62;transition:opacity .4s ease}
.card::before{content:"";position:absolute;left:0;top:12px;bottom:12px;width:3px;border-radius:2px;background:var(--c1);opacity:0;transition:opacity .4s ease}
.card.on b,.card.on p,.card.on::before{opacity:1}
.card b{display:block;margin-bottom:4px;font-size:12px;line-height:1.2;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#d0d2de}
.card p{text-wrap:pretty}
-/* phones: the chart is at the top; each card slides up below it, stops at the middle and stays until the next card slides over it */
-@media(max-width:639px){.steps{width:100%;padding-left:14px;padding-right:14px}.card{position:sticky;top:var(--stick,50vh);padding:11px 14px 12px}.card.past{opacity:0;transition-delay:.12s}body::after{display:none}}
+/* phones (the script sets .phone by the size of the box): the chart is at the top; each card slides up below it, stops at the middle and stays until the next card slides over it */
+.phone .steps{width:100%;padding-left:14px;padding-right:14px}
+.phone .card{position:sticky;top:var(--stick,50vh);padding:11px 14px 12px}
+.phone .card.past{opacity:0;transition-delay:.12s}
+.phone body::after{display:none}
+/* boxes too short for the cards at full size (.micro): smaller cards without the step label, and smaller numbers under the chart on phones */
+.micro .card{padding:7px 11px 8px;font-size:13px;line-height:1.3}
+.micro .card b{display:none}
+.micro.phone .l{font-size:10px}
@media(prefers-reduced-motion:reduce){*,::before,::after{transition:none!important}}
</style>
<script>
@@ -55,6 +62,7 @@ body::after{content:"";position:fixed;left:0;top:0;width:min(23em,40%);height:72
let lite;
try { lite = window.parent.matchMedia(q).matches; } catch (e) { lite = screen.width <= 600 || screen.height <= 500; }
document.documentElement.classList.toggle('lite', lite);
+ document.documentElement.classList.toggle('phone', innerWidth < 480 || (innerWidth < 640 && innerHeight >= 330));
}
</script>
</head>
@@ -72,7 +80,7 @@ body::after{content:"";position:fixed;left:0;top:0;width:min(23em,40%);height:72
</ol>
</main>
<script>
-const $ = s => document.querySelector(s);
+const $ = s => document.querySelector(s), root = document.documentElement;
const stage = $('.stage'), panel = $('.panel'), steps = $('.steps'), cards = [...document.querySelectorAll('.card')], N = cards.length;
const reduce = matchMedia('(prefers-reduced-motion: reduce)'), FRAME = 1000 / 60;
const COUNTS = [2, 4, 7, 9, 8, 5, 3, 2], BUSY = [2, 3, 4], NW = COUNTS.length; // made-up samples per week; the three busiest weeks
@@ -91,14 +99,19 @@ dots.forEach((d, i) => { d.cell = cells[i]; });
// Where everything sits at this size: the chart goes to the right of the cards, or on phones to the top above them. The dots form 1 or 2 sub-columns
// per week, whichever lets them be biggest.
-let G = {};
+let G = {}, phone = false, stick = 0;
function geo() {
- const W = innerWidth, H = stage.clientHeight, narrow = W < 640, tiny = H < 330, TOP = tiny ? 8 : 30;
- const ax0 = narrow ? 14 : Math.round(Math.min(W * .43, 440)), ax1 = W - (narrow ? 14 : 22), ay0 = 46, ay1 = narrow ? Math.round(H * .46) - 8 : H - 14;
- const cx0 = ax0 + 14, cx1 = ax1 - 14, fit = [1, 2].map(s => { const rows = Math.ceil(9 / s); return { s, rows, u: Math.max(4, Math.min((ay1 - ay0 - TOP - 30) / (rows + .4), (cx1 - cx0) / (NW * (s + .9)), 44)) }; }).sort((a, b) => b.u - a.u)[0];
- const { s, rows, u } = fit, ph = rows * u + TOP + 30, top = narrow ? ay0 : ay0 + Math.max(0, (ay1 - ay0 - ph) / 2);
+ const W = innerWidth, H = stage.clientHeight, tall = () => Math.max(...cards.map(c => c.offsetHeight));
+ phone = W < 480 || (W < 640 && H >= 330); // chart above the cards only when the box is narrow and tall enough for both
+ root.classList.toggle('phone', phone); root.classList.remove('micro');
+ if (tall() + H * .5 > H - 4) root.classList.add('micro');
+ const micro = root.classList.contains('micro'), mp = phone && micro, tiny = H < 330, TOP = tiny ? 8 : 30, LB = mp ? 5 : 9, BOT = LB + (mp ? 10 : 12) + 9;
+ stick = Math.round(Math.min(H * (micro ? .6 : .5), H - tall() - 6)); // on phones, the middle: where a card stops
+ const ax0 = phone ? 14 : Math.round(Math.min(W * .43, 440)), ax1 = W - (phone ? 14 : 22), ay0 = mp ? 40 : 46, ay1 = phone ? stick - 8 : H - 14;
+ const cx0 = ax0 + 14, cx1 = ax1 - 14, fit = [1, 2].map(s => { const rows = Math.ceil(9 / s); return { s, rows, u: Math.max(4, Math.min((ay1 - ay0 - TOP - BOT) / (rows + .4), (cx1 - cx0) / (NW * (s + .9)), 44)) }; }).sort((a, b) => b.u - a.u)[0];
+ const { s, rows, u } = fit, ph = rows * u + TOP + BOT, top = phone ? ay0 : ay0 + Math.max(0, (ay1 - ay0 - ph) / 2);
const cw = Math.min((cx1 - cx0) / NW, (s + 1.4) * u);
- G = { s, u, cw, cx0, cx1, top, ry: top + TOP, base: top + TOP + rows * u, x0: (cx0 + cx1) / 2 - NW * cw / 2, bc: Math.max(4, Math.ceil(24 / rows)) };
+ G = { s, u, cw, cx0, cx1, top, lb: LB, ry: top + TOP, base: top + TOP + rows * u, x0: (cx0 + cx1) / 2 - NW * cw / 2, bc: Math.max(4, Math.ceil(24 / rows)) };
stage.classList.toggle('tiny', tiny);
panel.style.cssText = `left:${ax0}px;top:${top}px;width:${ax1 - ax0}px;height:${ph}px`;
ct.style.cssText = `left:${ax0 + 14}px;top:${top + 10}px`;
@@ -113,7 +126,7 @@ function at(d, k) { // a dot's place in step k: scattered, in its week, in its
}
const px = (x, y) => `translate(${x.toFixed(1)}px,${y.toFixed(1)}px)`;
function show(k, snap) {
- if (snap) stage.classList.add('snap');
+ if (snap) root.classList.add('snap');
stage.dataset.k = k; stage.dataset.ch = k ? (k < 4 ? 1 : 2) : 0; stage.setAttribute('aria-label', SAY[k]);
const r = G.u * .4, lag = c => ((c / (NW - 1)) * .45).toFixed(2) + 's'; // the weeks fill one after another, left to right
dots.forEach(d => {
@@ -122,23 +135,23 @@ function show(k, snap) {
e.style.transform = px(x - r, y - r); e.style.opacity = k === 0 ? .7 : fade ? .26 : 1;
e.classList.toggle('a', k > 1 && d.busy); e.classList.toggle('b', k === 4 && !d.busy);
});
- [wl, vl].forEach(set => set.forEach((e, i) => { e.style.transform = px(G.x0 + (i + .5) * G.cw, G.base + 9); })); // the weeks' numbers, and the same numbers in sorted order
- gl.forEach((e, i) => { e.style.transform = px(G.cx0 + (G.cx1 - G.cx0) * (i ? .72 : .28), G.base + 9); });
+ [wl, vl].forEach(set => set.forEach((e, i) => { e.style.transform = px(G.x0 + (i + .5) * G.cw, G.base + G.lb); })); // the weeks' numbers, and the same numbers in sorted order
+ gl.forEach((e, i) => { e.style.transform = px(G.cx0 + (G.cx1 - G.cx0) * (i ? .72 : .28), G.base + G.lb); });
ax.style.transform = px(G.x0 - 4, G.base + 1);
cards.forEach((c, i) => { c.classList.toggle('on', i === k - 1); c.classList.toggle('past', i < k - 1); });
- if (snap) { void stage.offsetWidth; stage.classList.remove('snap'); }
+ if (snap) { void stage.offsetWidth; root.classList.remove('snap'); }
}
// The step is the last card that has reached the middle of the box, so scrolling back undoes each step at the same place.
let cur = -1, trig = [], END = 0, lastY = 0, raf = 0, lastT = null, playing = false, pos = 0, acted = false;
function measure() {
- const H = innerHeight, phone = innerWidth < 640; // the first card waits below the middle, fully in view when the box is tall enough for it
- let y = Math.max(H * .5 + 8, Math.min(H * .64, H - cards[0].offsetHeight - 12));
+ const H = innerHeight, TL = phone ? stick : H * .5, cN = cards[N - 1].offsetHeight; // TL: where a card's top must be to start its step
+ let y = Math.max(TL + 3, Math.min(H * .64, H - cards[0].offsetHeight - 8)); // the first card waits below that line, fully in view when it fits
steps.style.paddingTop = y + 'px';
- steps.style.setProperty('--stick', H * .5 + 'px');
- steps.style.setProperty('--endh', Math.max(0, phone ? H * .5 : H * .54 - cards[N - 1].offsetHeight) + 'px');
+ steps.style.setProperty('--stick', stick + 'px');
+ steps.style.setProperty('--endh', (phone ? Math.max(0, H - cN - stick) + H * .1 : Math.max(0, H * .54 - cN)) + 'px');
END = Math.max(0, document.documentElement.scrollHeight - H);
- trig = cards.map(c => { const t = y - H * .5; y += c.offsetHeight + parseFloat(getComputedStyle(c).marginBottom); return t; }); // where each card would be if nothing stuck
+ trig = cards.map((c, i) => { if (i) y += parseFloat(getComputedStyle(c).marginTop); const t = y - TL; y += c.offsetHeight; return t; }); // where each card would be if nothing stuck
}
function check(snap) {
const y = scrollY; let k = 0;
@@ -177,7 +190,7 @@ addEventListener('scroll', () => check(false), { passive: true });
addEventListener('resize', () => { geo(); measure(); cur = -1; check(true); });
document.addEventListener('visibilitychange', () => { lastT = null; });
geo(); measure(); check(true);
-if (document.fonts) document.fonts.ready.then(() => { measure(); check(false); }); // the cards may change height once the font is in
+if (document.fonts) document.fonts.ready.then(() => { geo(); measure(); cur = -1; check(true); }); // the cards may change height once the font is in
addEventListener('load', () => {
measure(); check(false);
setTimeout(() => { if (!acted && !reduce.matches && scrollY < 2) play(); }, 700);