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(scroll-trigger): start each section's scrub as it enters, not once it tops out

matin9d77e2945d47
diff --git a/animations/01-scroll-based/scroll-trigger/README.md b/animations/01-scroll-based/scroll-trigger/README.md
index fa6f2e5c..c2512e2b 100644
--- a/animations/01-scroll-based/scroll-trigger/README.md
+++ b/animations/01-scroll-based/scroll-trigger/README.md
@@ -51,13 +51,15 @@ swatch.style.filter = `hue-rotate(${p * 120}deg)`;
|-----------|---------|--------|
| Zone start | zone top, measured against the stage | Where the trigger fires. Must be in the scroll container's coordinate space, not `offsetTop` |
| Zone height | 550px | The span over which a zone counts as active |
-| Scrub denominator | `zoneHeight − viewport × 0.5` | How much scrolling maps to 0→1 progress |
+| Scrub start | `top 80%` | Zone top at 80% down the viewport — just after the section appears |
+| Scrub end | `top 20%` | Zone top near the viewport top; the animation finishes as the section settles |
| Run-out | one viewport | Trailing space so the final zone can exit and fire `onLeave` |
## Production notes
- **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.
- **`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 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. 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.html
index 51d3f072..af19601e 100644
--- a/animations/01-scroll-based/scroll-trigger/index.html
+++ b/animations/01-scroll-based/scroll-trigger/index.html
@@ -169,7 +169,7 @@ body{padding-top:46px}
const pinStep=document.getElementById('pin-step'),pinTitle=document.getElementById('pin-title'),pinBody=document.getElementById('pin-body');
const snapDots4=[...document.getElementById('snap-dots-z4').children],snapVal=document.getElementById('snap-val');
const pills=[document.getElementById('pill1'),document.getElementById('pill2'),document.getElementById('pill3'),document.getElementById('pill4')];
- const cbLog=document.getElementById('cb-log');
+ const cbLog=document.getElementById('cb-log'),EMPTY_LOG=cbLog.innerHTML;
const PIN_STEPS=[['Step 1 of 3','Initialize','The trigger fires. The animation begins. State is set.'],['Step 2 of 3','Process','Scroll-linked progress drives every value. Each pixel is intentional.'],['Step 3 of 3','Complete','The trigger fires onLeave. State is captured. Callback logged.']];
let ticking=false,prevState=[null,null,null,null],cbEvents=[],primed=false;
const clamp=(v,a,b)=>Math.min(Math.max(v,a),b);
@@ -191,6 +191,14 @@ body{padding-top:46px}
}
const zoneState=i=>{const vt=stage.scrollTop;
if(vt+VH<=ZT[i])return'idle'; if(vt>=ZT[i]+ZH[i])return'past'; return'active'};
+
+ /* Scrub window, in GSAP's terms `start:"top 80%"` / `end:"top 20%"` — progress
+ begins once the zone's top has risen to 80% down the viewport (just after it
+ appears) and completes as it settles near the top. Measuring from the zone top
+ alone, as this did before, held progress at 0 until the section had already
+ scrolled fully into view — roughly 0.8 of a viewport late. */
+ const START=0.8,END=0.2;
+ const scrub=i=>clamp((stage.scrollTop-(ZT[i]-START*VH))/((START-END)*VH),0,1);
function logEvent(msg){cbEvents=[msg,...cbEvents].slice(0,5);cbLog.innerHTML=cbEvents.map(e=>`<span>${e}</span>`).join('')}
const NAMES=['Fade on Enter','Scrub','Pin + Scrub','Snap'];
@@ -222,19 +230,19 @@ body{padding-top:46px}
// Zone 1 — fade on enter
set('reveal',states[0]==='active',v=>reveal.classList.toggle('active',v));
// Zone 2 — scrub
- const p2=clamp((st-ZT[1])/(ZH[1]-VH*.5),0,1);
+ const p2=scrub(1);
set('p2',Math.round(p2*200)/200,v=>{
swatch.style.transform=`rotate(${lerp(0,180,v)}deg) scale(${lerp(1,1.3,v)})`;
swatch.style.filter=`hue-rotate(${lerp(0,120,v)}deg)`;
scrubVal.textContent=`progress: ${Math.round(v*100)}%`;
});
// Zone 3 — pin + scrub
- const p3=clamp((st-ZT[2])/(ZH[2]-VH*.5),0,1);
+ const p3=scrub(2);
set('p3',Math.min(2,Math.floor(p3*3)),v=>{
pinStep.textContent=PIN_STEPS[v][0];pinTitle.textContent=PIN_STEPS[v][1];pinBody.textContent=PIN_STEPS[v][2];
});
// Zone 4 — snap
- const p4=clamp((st-ZT[3])/ZH[3],0,1);
+ const p4=scrub(3);
set('p4',Math.round(p4*5),v=>{
snapDots4.forEach((d,i)=>{d.classList.toggle('lit',i<v);d.style.background=i<v?'#f85149':'var(--ui-border)'});
snapVal.textContent=`${v} / 5 lit`;
@@ -243,7 +251,7 @@ body{padding-top:46px}
}
stage.addEventListener('scroll',()=>{if(!ticking){requestAnimationFrame(update);ticking=true}},{passive:true});
document.getElementById('reset-btn').addEventListener('click',()=>{
- stage.scrollTop=0;cbEvents=[];cbLog.innerHTML='';primed=false;measure();update();
+ stage.scrollTop=0;cbEvents=[];cbLog.innerHTML=EMPTY_LOG;primed=false;measure();update();
});
let rt;addEventListener('resize',()=>{clearTimeout(rt);rt=setTimeout(()=>{measure();update()},150)});
measure();update();