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(stacking-cards): keep the finished deck on screen at the end of the scroll
matinc2a1b7817bac
diff --git a/animations/01-scroll-based/stacking-cards/README.md b/animations/01-scroll-based/stacking-cards/README.mdindex c389348e..17ca33b1 100644--- a/animations/01-scroll-based/stacking-cards/README.md+++ b/animations/01-scroll-based/stacking-cards/README.md@@ -66,6 +66,8 @@ content.style.transform = `scale(${lerp(1, 1 + step - step * (N - i), t)})`;- **CSS does the work; JS adds polish.** The stacking is correct with zero JavaScript — cards appear in the right order purely from `position: sticky` and `z-index`. The scroll-driven timeline supplies the depth cue (scale), and the JavaScript path exists only for browsers without `animation-timeline`.- **`transform: scale()` is compositor-only.** It triggers neither layout nor paint, which is why the depth cue stays cheap even with every card animating at once.+- **The run-out has to be inside the sticky parent.** A sticky element unsticks as soon as its own bottom reaches the bottom of its containing block. The last card's bottom is flush with the end of the deck, so it is pinned for precisely zero pixels — reach the end and the finished stack slides straight off the top, leaving an empty stage. Putting the trailing space *after* the container does not help, and neither does `padding-bottom` on the container: the sticky constraint is the containing block's **content** box, which excludes padding. It must be a real child element after the cards. Its height is exactly how far you can keep scrolling with the completed deck held on screen.+- **Size cards to include their own offset.** Each card is content plus its `padding-top` peek, and the deepest card carries the tallest stack of it. Sizing content to a flat `stage − 80px` means the last card overflows the container once `(N−1) × peek` exceeds that slack — at 7 cards it clipped. Subtracting the peek run instead (`stage − 24px − (N−1) × peek`) keeps every card within the stage at any count or peek value.- **Never read `offsetTop` from a sticky element.** This is the trap in this pattern. `position: sticky` changes an element's *used* position, so `offsetTop` reports where the card is currently stuck, not where it sits in flow — the value moves as you scroll, and a measurement taken mid-scroll comes back with offsets that run backwards. Accumulate `offsetHeight` instead (sticky does not affect it), and take the container's origin from a non-sticky ancestor.- **Watch the coordinate space.** `offsetTop` is measured from the nearest positioned ancestor — usually `body` — while `scrollTop` is measured from the scroll container's content origin. Mixing them silently offsets everything by the page chrome above the container. If a parent and child share an `offsetParent`, adding their `offsetTop`s double-counts.- **Keep the fallback honest.** A JS fallback that merely "looks about right" is worse than none, because nobody re-checks it. Diff its output against the native timeline at several scroll positions — here the two agree to within rounding at every point.diff --git a/animations/01-scroll-based/stacking-cards/index.html b/animations/01-scroll-based/stacking-cards/index.htmlindex eed643d0..529ab7c8 100644--- a/animations/01-scroll-based/stacking-cards/index.html+++ b/animations/01-scroll-based/stacking-cards/index.html@@ -33,8 +33,18 @@/* stage: the ONLY scroll container. no overflow on any descendant above .card */.stage{flex:1;min-width:0;height:var(--stage-h);overflow-y:scroll;scrollbar-width:none;border:1px solid var(--ui-border);border-radius:8px;background:var(--bg)}.stage::-webkit-scrollbar{display:none}- .spacer{height:500px}+ /* lead-in: enough to scroll the first card up into the stack, but not so much+ that the demo opens on an empty stage */+ .spacer{height:calc(var(--stage-h) * 0.25)}#cards{--numcards:5;view-timeline-name:--cards-timeline;view-timeline-axis:block}+ /* The run-out MUST be a real child of #cards. A sticky card stays pinned only+ while its containing block extends below it, and the last card's bottom is+ flush with the last card — so a spacer *after* #cards let the finished deck+ slide straight off the top. Padding does not work either: the sticky+ constraint is the containing block's content box, which excludes padding.+ This element's height is the pinned window — how far you can keep scrolling+ with the completed deck held on screen. */+ .run-out{height:calc(var(--stage-h) * 0.3)}/* sticky is on .card — the outer wrapper — NOT on .card__content */.card{--index0:calc(var(--index) - 1);@@ -46,7 +56,10 @@z-index:var(--index);}.card__content{- height:calc(var(--stage-h) - 80px);+ /* Each card's total height is content + its peek padding, and the last card+ carries the tallest stack of it. Subtracting the peek run keeps every card+ within the stage, so the deepest card never clips at high card counts. */+ height:calc(var(--stage-h) - 24px - (var(--numcards) - 1) * var(--peek));border-radius:16px;overflow:hidden;display:grid;grid-template-columns:55fr 45fr;box-shadow:0 16px 56px rgba(0,0,0,.65);@@ -121,7 +134,6 @@ body{padding-top:46px}<div class="stage" id="stage"><div class="spacer"></div><div id="cards"></div>- <div class="spacer"></div></div><aside><p class="note">Sticky is on .card (not .card__content). padding-top per index peeks earlier cards above later ones. transform-origin:50% 0% collapses scale downward so peek tabs stay visible.</p>@@ -187,6 +199,9 @@ body{padding-top:46px}<div class="card__visual" style="background:${d.vis}"></div></div>`;cardsEl.appendChild(el);currentCards.push(el);});+ // run-out lives inside #cards so the last card can stay pinned to the end+ const runOut=document.createElement('div');runOut.className='run-out';+ cardsEl.appendChild(runOut);contents=currentCards.map(c=>c.querySelector('.card__content'));lastScales=currentCards.map(()=>-1);cardsEl.classList.toggle('js-fallback',useJsFallback);