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
fix: keep ScrollTrigger's turning square clear of its caption
matin0e24646200f1
diff --git a/animations/01-scroll-based/scroll-trigger/README.md b/animations/01-scroll-based/scroll-trigger/README.mdindex 0013d2b9..095dbd58 100644--- a/animations/01-scroll-based/scroll-trigger/README.md+++ b/animations/01-scroll-based/scroll-trigger/README.md@@ -37,7 +37,7 @@ if (s !== prev[i]) {}```-Scrubbing uses the same scroll position, normalised to `0–1` and clamped, then applied directly to transforms. Because it is clamped rather than gated on the active state, it resolves to 0 before its window and 1 after it — so a zone you skipped past still ends up in its finished state. The window belongs to the piece that animates (the square, the row of dots), not to the zone around it: here a zone is nearly a box tall and centres its content, so a window keyed to the zone's top would run half a box before the piece could be seen:+Scrubbing uses the same scroll position, normalised to `0–1` and clamped, then applied directly to transforms. Because it is clamped rather than gated on the active state, it resolves to 0 before its window and 1 after it — so a zone you skipped past still ends up in its finished state. The window belongs to the piece that animates (the square, the row of dots), not to the zone around it: here a zone is nearly a box tall and centres its content, so a window keyed to the zone's top would run before the piece could be seen:```js// follow window: 0 as the piece's centre passes 80% down the box, 1 at 25%@@ -69,7 +69,7 @@ const p = clamp((stage.scrollTop - top[i]) / (height[i] - viewport), 0, 1);- **GSAP ScrollTrigger** is the production standard. It handles scroll direction, pinning, scrubbing, snapping, and lifecycle callbacks with a clean declarative API. The vanilla JS approach here is educational — it replicates the underlying mechanics without GSAP's optimizations.- **`IntersectionObserver` vs. scroll events.** IO is the right tool for lifecycle callbacks (enter/leave); scroll-position arithmetic is right for scrubbing. This demo derives both from scroll position so the two stay in lockstep — with IO the callback and the scrub can disagree by a frame.-- **Anchor the scrub to where the section *enters*, not to where it reaches the top.** Measuring progress as `scrollTop − zoneTop` keeps it at zero until the section's top has climbed all the way to the top of the viewport — by which point the reader has watched a full screen of it sit motionless, and the animation only plays as it leaves. That reads as a broken or badly late trigger. GSAP's default `start: "top 80%"` / `end: "top 20%"` begins just after the section appears and finishes as it settles; here that moved each trigger roughly 0.8 of a viewport earlier. Anchor it to the piece that animates, too: a zone that is nearly a box tall and centres its content puts the piece half a box below the zone's top, so the demo uses `start: "center 80%"` / `end: "center 25%"` on the square and on the row of dots, and the animation plays where it can be seen.+- **Anchor the scrub to where the section *enters*, not to where it reaches the top.** Measuring progress as `scrollTop − zoneTop` keeps it at zero until the section's top has climbed all the way to the top of the viewport — by which point the reader has watched a full screen of it sit motionless, and the animation only plays as it leaves. That reads as a broken or badly late trigger. A common GSAP setting, `start: "top 80%"` / `end: "top 20%"`, begins just after the section appears and finishes as it settles; here that moved each trigger roughly 0.8 of a viewport earlier. Anchor it to the piece that animates, too: a zone that is nearly a box tall and centres its content puts the piece half a box below the zone's top, so the demo uses `start: "center 80%"` / `end: "center 25%"` on the square and on the row of dots, and the animation plays where it can be seen.- **`offsetTop` is not in the scroll container's coordinate space.** It is measured from the nearest *positioned* ancestor, which for a plain `overflow: scroll` panel is usually `body` — so it includes every pixel of page chrome above the container, while `scrollTop` starts at zero inside it. Comparing them directly put every trigger here 109px late. Measure the zone against the container's own box instead.- **Prime the state before firing callbacks.** Comparing the first frame's state against a `null` starting value manufactures events that never happened: zone 1 would fire `onEnterBack` and zones 2–4 `onLeaveBack` before any scrolling. Record the initial state on the first pass and only emit transitions after that.- **Evaluate every trigger each frame, not just the active ones.** Updating a zone only while it is active leaves whatever value it happened to stop on — jump past a zone and its scrub never runs at all, and scrolling back to the top left the pinned card still reading "Step 3 of 3". Clamped progress resolves to 0 before a zone and 1 after it, so both ends settle correctly on their own.diff --git a/animations/01-scroll-based/scroll-trigger/index.html b/animations/01-scroll-based/scroll-trigger/index.htmlindex ae0f626d..d3c59348 100644--- a/animations/01-scroll-based/scroll-trigger/index.html+++ b/animations/01-scroll-based/scroll-trigger/index.html@@ -46,7 +46,7 @@.reveal-title{font-size:clamp(18px,3vw,26px);font-weight:bold;margin-bottom:8px}.reveal-sub{font-size:14px;color:var(--ui-muted);line-height:1.6}/* Zone 2 */- .swatch{width:90px;height:90px;border-radius:12px;background:var(--ui-accent);will-change:transform,filter;margin:24px auto}+ .swatch{width:90px;height:90px;border-radius:12px;background:var(--ui-accent);will-change:transform,filter;margin:40px auto 24px}/* 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}@@ -84,7 +84,7 @@<span class="zone-tag">Fades in</span><div class="reveal-content" id="reveal"><div class="reveal-title">Fades in on arrival</div>- <div class="reveal-sub">This text fades in when its zone scrolls into the box, and hides again once the zone has gone.</div>+ <div class="reveal-sub">This text fades in as it scrolls into the box, and hides again once it has gone.</div></div></div><div class="zone z2" id="zone2">diff --git a/docs/superpowers/plans/2026-09-29-scroll-content-part1.md b/docs/superpowers/plans/2026-09-29-scroll-content-part1.mdindex c83225a6..4f41b92f 100644--- a/docs/superpowers/plans/2026-09-29-scroll-content-part1.md+++ b/docs/superpowers/plans/2026-09-29-scroll-content-part1.md@@ -534,7 +534,7 @@ None: leave out the `details.hb-options` block.- `.stage`: `position:relative;overflow-y:auto;scrollbar-width:none;container-type:size;background:#0b0b0d`, with `aria-label="Four scroll zones"`.- Sizes: `.zone{min-height:90cqh}` (550px in a 620px stage today); `.z3{min-height:200cqh}`; `.pin-card{top:calc(50cqh - 70px)}`; `.run-out{height:100cqh}`.- Zone labels (`.zone-tag`, 11px, was 9px): "Fades in", "Follows the scroll", "Pinned", "Steps" (were "Fade on Enter", "Scrub", "Pin + Scrub", "Snap"). Their `opacity:.8` goes: text faded below 4.5:1 is fixed in its section, and with it the four labels measure 4.22, 4.31, 4.22 and 3.47:1 (text and tinted background both fade over the zone), without it 5.74, 5.86, 5.75 and 4.68:1.- - Zone 1: the heading reads "Fades in on arrival" and the text "This text fades in when its zone scrolls into the box, and hides again once the zone has gone." (today's text names onEnter and onLeave). `.reveal-sub` becomes 14px (was 13px). The fade is keyed to the text's own layout box (`#reveal`, by `offsetTop` and `offsetHeight`, which its 30px translate before it fades in does not change): active while any part of that box is in the box, not while any part of the zone is. Keyed to the zone, the text turned active when the zone's bottom edge came into the box, with the text still 84–138px above it, so scrolling back up showed no fade. On load, after Back to top and when Play restarts, the text is in the box at once and fades in as before.+ - Zone 1: the heading reads "Fades in on arrival" and the text "This text fades in as it scrolls into the box, and hides again once it has gone." (today's text names onEnter and onLeave). `.reveal-sub` becomes 14px (was 13px). The fade is keyed to the text's own layout box (`#reveal`, by `offsetTop` and `offsetHeight`, which its 30px translate before it fades in does not change): active while any part of that box is in the box, not while any part of the zone is. Keyed to the zone, the text turned active when the zone's bottom edge came into the box, with the text still 84–138px above it, so scrolling back up showed no fade. On load, after Back to top and when Play restarts, the text is in the box at once and fades in as before.- Zone 2: the caption reads "The square turns, grows and changes color as you scroll." The "progress: 0%" readout under the square goes.- Zone 3: `PIN_STEPS` becomes:@@ -548,7 +548,7 @@ None: leave out the `details.hb-options` block.- Zone 4: the caption reads "Scroll on to light up the dots, one at a time." The "0 / 5 lit" readout goes.- The two captions' inline styles move into a `.zone-cap` rule (13px, `var(--ui-muted)`, the zone 2 caption with `margin-bottom:4px`).- The run-out reads "That's the end · scroll back up to see each zone again" in the cue style (shared rule 14): sentence case (its `text-transform:uppercase` goes), `text-align:center;padding:0 24px`, 13px `#8a8a92` with `letter-spacing:.1em`, without the opacity .5 (today 10px capitals at about 2:1, left-aligned against the edge on phones).- - Every zone's window is reached at every size, and happens where the visitor can see it (the binding rule: the effect is seen during Play and while scrolling by hand). The square and the row of dots each follow their own centre from 80% to 25% down the box: `clamp((scrollTop - (centre - 0.8 * box)) / (0.55 * box), 0, 1)`. A window keyed to the zone's top (80% to 20%, as the old page had it) ran about half a box too early, because a zone is 90% of the box tall and centres its content: dot 1 lit with the row 61–96px below the box, the row came into view with two or three dots already lit, and the square started turning below the box. An end line of 20% would also leave the finished square 7px above the 258px box; 25% keeps it inside (measured in boxes of 258–438px and on phones: dot 1 lights with the row centre at 74–75% down the box, dot 5 at 30–31%; the square starts turning at 161–252px of the 258px box and is done at 6–123px, inside the box for the whole turn). After zone 4 the one-box run-out lets the row rise well past 25% of the box, so all five dots light, and the pinned card's three stages fit inside zone 3's span (shared rule 4). In the 258px box of a 1280×590 window the zones are 232px, zone 2's content 154px (170px on phones, where its caption wraps) and the pinned card, 136px tall with two lines of text at every width, sits 59–195px down the box (measured; phones 79–215px of 298).+ - Every zone's window is reached at every size, and happens where the visitor can see it (the binding rule: the effect is seen during Play and while scrolling by hand). The square and the row of dots each follow their own centre from 80% to 25% down the box: `clamp((scrollTop - (centre - 0.8 * box)) / (0.55 * box), 0, 1)`. A window keyed to the zone's top (80% to 20%, as the old page had it) ran about half a box too early, because a zone is 90% of the box tall and centres its content: dot 1 lit with the row 61–96px below the box, the row came into view with two or three dots already lit, and the square started turning below the box. An end line of 20% would also leave the finished square 7px above the 258px box; 25% keeps it inside (measured in boxes of 258–438px and on phones: dot 1 lights with the row centre at 74–75% down the box, dot 5 at 30–31%; the square starts turning at 161–252px of the 258px box and is done at 6–123px, inside the box for the whole turn). After zone 4 the one-box run-out lets the row rise well past 25% of the box, so all five dots light, and the pinned card's three stages fit inside zone 3's span (shared rule 4). In the 258px box of a 1280×590 window the zones are 232px (zone 2 is 236px: its content is taller), zone 2's content 170px (186px on phones, where its caption wraps; the square sits 40px under its caption: turned and grown it reaches 33px beyond its box, so it stays at least 7px clear of the caption) and the pinned card, 136px tall with two lines of text at every width, sits 59–195px down the box (measured; phones 79–215px of 298).- Script: `update()` keeps the zone states, zone 1's class, the square, the card steps and the dots. The pills, the callback log and the progress readout go, with `logEvent()`, `NAMES`, `prevState`, `primed`, `cbEvents` and `EMPTY_LOG`. `measure()` also runs after `document.fonts.ready`, measures the zone tops against the stage's inner edge (`clientTop`, the 1px border) so they match `scrollTop`, and caches the place of the text (`#reveal`), the centre of the square and the centre of the row of dots from their layout boxes (the zone top plus `offsetTop`; `offsetHeight` for the height and the centre), not from their on-screen rectangles, which the turn, the scale and the 30px translate change.- `hb-dots`: no. Default height.- **Removed:**@@ -581,7 +581,7 @@ None: leave out the `details.hb-options` block.- **README How it works:**- "A change of state is a callback. Which one depends on the direction the state moved:" becomes "A change of state is a callback. Which one depends on the direction the state moved (the demo shows them through the text in zone 1, which fades in on enter and hides on leave):".- In that snippet, `log(ev); prev[i] = s;` becomes `prev[i] = s; // run the zone's enter or leave animation for ev`.- - In the sentence before the scrub snippet, "it resolves to 0 before the zone and 1 after it — so a zone you skipped past still ends up in its finished state:" becomes "it resolves to 0 before its window and 1 after it — so a zone you skipped past still ends up in its finished state. The window belongs to the piece that animates (the square, the row of dots), not to the zone around it: here a zone is nearly a box tall and centres its content, so a window keyed to the zone's top would run half a box before the piece could be seen:". In the snippet, the two lines `// enter window: 0 as the zone top passes 80% down the viewport, 1 at 20%` and `const p = clamp((stage.scrollTop - (top[i] - 0.8 * viewport)) / (0.6 * viewport), 0, 1);` become `// follow window: 0 as the piece's centre passes 80% down the box, 1 at 25%`, `// (centre: the middle of its layout box, measured against the stage and cached like the zone tops)` and `const p = clamp((stage.scrollTop - (centre - 0.8 * viewport)) / (0.55 * viewport), 0, 1);`.+ - In the sentence before the scrub snippet, "it resolves to 0 before the zone and 1 after it — so a zone you skipped past still ends up in its finished state:" becomes "it resolves to 0 before its window and 1 after it — so a zone you skipped past still ends up in its finished state. The window belongs to the piece that animates (the square, the row of dots), not to the zone around it: here a zone is nearly a box tall and centres its content, so a window keyed to the zone's top would run before the piece could be seen:". In the snippet, the two lines `// enter window: 0 as the zone top passes 80% down the viewport, 1 at 20%` and `const p = clamp((stage.scrollTop - (top[i] - 0.8 * viewport)) / (0.6 * viewport), 0, 1);` become `// follow window: 0 as the piece's centre passes 80% down the box, 1 at 25%`, `// (centre: the middle of its layout box, measured against the stage and cached like the zone tops)` and `const p = clamp((stage.scrollTop - (centre - 0.8 * viewport)) / (0.55 * viewport), 0, 1);`.- The rest is unchanged.- **README Production notes:** two bullets lose the removed log and readout, and a third names the piece's window:- "**Prime the state before logging callbacks.** Comparing the first frame's state against a `null` starting value manufactures events that never happened: this log opened claiming zone 1 had fired `onEnterBack` and zones 2–4 `onLeaveBack`, before any scrolling." becomes "**Prime the state before firing callbacks.** Comparing the first frame's state against a `null` starting value manufactures events that never happened: zone 1 would fire `onEnterBack` and zones 2–4 `onLeaveBack` before any scrolling." Its last sentence stays.@@ -590,6 +590,7 @@ None: leave out the `details.hb-options` block.- The rest is unchanged.- **Category line:** `01.07 · Scroll-Based`- **Pager:** Previous: Stacking Cards (`../stacking-cards/`) · Next: Scrub Animation (`../scrub-animation/`)+- **Final fix wave (2026-09-30):** the turning square cut into its caption (turned and grown it reaches 33px beyond its box, and the gap was 24px; on phones it covered "you scroll."). `.swatch` is now `margin:40px auto 24px`: measured over the whole follow window at eight sizes, the square stays at least 7px clear of the caption's line (30px would still overlap it by 3px), and only the 258px box changes (zone 2 is 236px there, the scroll 3px longer). Zone 1's stage text now says the fade is keyed to the text itself, and two README wordings are corrected ("half a box", and GSAP's "default" start and end).---