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: Smooth Scroll glides for the same time on every screen
matin6400a02cf3a7
diff --git a/animations/01-scroll-based/smooth-scroll/README.md b/animations/01-scroll-based/smooth-scroll/README.mdindex 7d8a3007..01826a85 100644--- a/animations/01-scroll-based/smooth-scroll/README.md+++ b/animations/01-scroll-based/smooth-scroll/README.md@@ -27,13 +27,17 @@ viewport.addEventListener('wheel', e => {kick(); // starts the loop if it is idle}, { passive: false });-// each frame eases current toward target-function loop() {- current += (target - current) * ease; // ease ~0.09+// each frame eases current toward target, by a share that depends on how long the frame took+const FRAME = 1000 / 60; // ease is counted per 1/60 s+function loop(now) {+ const dt = last ? Math.min(now - last, 50) : FRAME; // time since the last frame; 1/60 s for the first+ last = now;+ current += (target - current) * (1 - Math.pow(1 - ease, dt / FRAME)); // ease ~0.09if (Math.abs(target - current) < 0.1) current = target;content.style.transform = `translate3d(0, ${-current}px, 0)`;if (current !== target) requestAnimationFrame(loop);}+function kick() { if (!raf) { last = 0; raf = requestAnimationFrame(loop); } }```Touch drag uses Pointer Events: on `pointerdown` the current target is captured, and `pointermove` offsets it by the drag distance, so the same target/current machinery serves both mouse wheel and finger drag. The gap between `target` and `current` is what produces momentum: a flick pushes `target` ahead, and `current` coasts after it until the two converge.@@ -42,13 +46,13 @@ The keyboard moves the same target: in the glide mode a `keydown` handler on theThe demo keeps all of this inside a scoped box with `overflow: hidden` — it never touches `window` scroll — so it embeds without hijacking the page.-A fixed lerp factor is frame-rate dependent — the same value settles faster on a 120Hz display than on 60Hz. For rate-independent easing, scale the factor by delta time: `1 - Math.pow(1 - ease, dt * 60)`.+The share moved each frame depends on how long the frame took. A fixed lerp factor per frame is frame-rate dependent: the same value settles twice as fast on a 120Hz display as on 60Hz, and half as fast on a 30Hz phone. Here `ease` is the share covered in 1/60 s, and a frame that lasted `dt` covers `1 - Math.pow(1 - ease, dt / FRAME)` of the distance left, so the distance left shrinks exponentially with time and the glide takes the same time on every screen. `kick()` clears `last`, so the first frame after a start counts as 1/60 s rather than the time since some old frame, and `dt` is capped at 50ms, so a hidden tab does not make the content jump.## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Glide | Medium | How much of the remaining distance the content covers each frame: long is 5%, medium 9% and short 16%; long feels heavy and floaty, short is closer to normal scrolling |+| Glide | Medium | How much of the remaining distance the content covers in a sixtieth of a second: long is 5%, medium 9% and short 16%; a longer frame covers more, so the glide takes as long on every screen; long feels heavy and floaty, short is closer to normal scrolling || Normal scrolling | off | Turns the glide off so the box scrolls the browser's own way, to compare the two |## Production notesdiff --git a/animations/01-scroll-based/smooth-scroll/index.html b/animations/01-scroll-based/smooth-scroll/index.htmlindex 30ca26c1..39261897 100644--- a/animations/01-scroll-based/smooth-scroll/index.html+++ b/animations/01-scroll-based/smooth-scroll/index.html@@ -113,7 +113,7 @@<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div></div><div class="hb-card">- <p class="hb-prompt">Add smooth scrolling to [the scrolling area or page you want to glide]. Catch the visitor's wheel, swipe and drag and turn each one into a target position, then on every frame move the content part of the remaining distance toward that target, so it glides and slows to a stop instead of jumping. Move the content with a transform and keep a custom scrollbar in step with it. Keep keyboard scrolling, links to headings and find-in-page working. If the visitor has reduced motion turned on, use the browser's normal scrolling with no glide. Match the settings listed below.</p>+ <p class="hb-prompt">Add smooth scrolling to [the scrolling area or page you want to glide]. Catch the visitor's wheel, swipe and drag and turn each one into a target position, then on every frame move the content part of the remaining distance toward that target, so it glides and slows to a stop instead of jumping. Make that part exponential in the time since the last frame (one minus a fixed fraction to the power of that time), so the pace is the same on every screen. Move the content with a transform and keep a custom scrollbar in step with it. Keep keyboard scrolling, links to headings and find-in-page working. If the visitor has reduced motion turned on, use the browser's normal scrolling with no glide. Match the settings listed below.</p><div class="hb-chips-box" hidden><p class="hb-chips-title">Your settings</p><ul class="hb-chips"></ul>@@ -149,7 +149,8 @@const fill=document.getElementById('fill'),hint=document.getElementById('hint'),nativeChk=document.getElementById('native-tog');const motionMQ=window.matchMedia('(prefers-reduced-motion: reduce)');const clamp=(v,a,b)=>Math.min(Math.max(v,a),b);- let target=0,current=0,ease=0.09,maxScroll=0,viewH=0,mode='',raf=0,dragging=false,startY=0,startTarget=0;+ const FRAME=1000/60; // ease is the share of the distance covered in 1/60 s: a 120Hz or a 30Hz screen glides for as long as a 60Hz one+ let target=0,current=0,ease=0.09,maxScroll=0,viewH=0,mode='',raf=0,last=0,dragging=false,startY=0,startTarget=0; // last: time of the previous frame, 0 when the loop has just startedlet showTimer=0,returning=false,runFrom=0,wasNative=false; // a Show me run: its timer, its glide back, where it began// The visitor pressed, typed, scrolled or touched inside the stage: a Show me run under way (waiting to return, or gliding@@ -170,16 +171,19 @@function draw(pos){content.style.transform=`translate3d(0,${(-pos).toFixed(2)}px,0)`;paint(pos)}// Smooth mode: ease current toward target each frame and apply it as a transform. The loop runs only while the two differ.- function loop(){+ // A frame that lasted dt covers 1 - (1 - ease)^(dt / FRAME) of the distance left; the first frame after a start counts as+ // 1/60 s (kick() clears last), and a long gap, such as a hidden tab, as 50 ms.+ function loop(now){raf=0;if(mode!=='smooth')return;- current+=(target-current)*ease;+ const dt=last?Math.min(now-last,50):FRAME;last=now;+ current+=(target-current)*(1-Math.pow(1-ease,dt/FRAME));if(Math.abs(target-current)<0.1)current=target;draw(current);if(current!==target)raf=requestAnimationFrame(loop);else returning=false;}- function kick(){if(!raf&&mode==='smooth')raf=requestAnimationFrame(loop)}+ function kick(){if(!raf&&mode==='smooth'){last=0;raf=requestAnimationFrame(loop)}}function onWheel(e){e.preventDefault();