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: Cover Card header follows the scroll at the same pace on every screen

matin6b6f198e3e1f
diff --git a/animations/01-scroll-based/cover-card-to-fixed-header/README.md b/animations/01-scroll-based/cover-card-to-fixed-header/README.md
index 956a1e5f..a16a003d 100644
--- a/animations/01-scroll-based/cover-card-to-fixed-header/README.md
+++ b/animations/01-scroll-based/cover-card-to-fixed-header/README.md
@@ -48,8 +48,19 @@ rather than by writing `font-size` — see the production note below. Nothing is
resized: the cover keeps its full height in the layout and slides up by what it
has shrunk, and the article slides with it; the badge and the backdrop are moved
back into the visible part, and once the cover is shorter than its text the text
-starts at the top of the bar. Back to top and Play from the end set the eased
-value at once.
+starts at the top of the bar.
+
+The scroll does not set `e` itself: it sets a target, and a loop moves the value
+that is drawn toward it, so a wheel notch glides instead of jumping. Each frame
+covers a share of the distance left that depends on how long the frame took, so
+the header settles in the same time on a 30, 60 or 120Hz screen:
+
+```js
+const dt = last ? Math.min(now - last, 50) : FRAME; // FRAME = 1000 / 60; the first frame after a restart counts as 1/60 s
+current += (target - current) * (1 - Math.pow(1 - EASE, dt / FRAME)); // EASE 0.16 is the share covered in 1/60 s
+```
+
+Back to top and Play from the end set the eased value at once.
## Key parameters
diff --git a/animations/01-scroll-based/cover-card-to-fixed-header/index.html b/animations/01-scroll-based/cover-card-to-fixed-header/index.html
index 58f5ef5b..0ea6da94 100644
--- a/animations/01-scroll-based/cover-card-to-fixed-header/index.html
+++ b/animations/01-scroll-based/cover-card-to-fixed-header/index.html
@@ -201,9 +201,9 @@
// A wheel notch is ~100px — nearly a third of RANGE — and easeOutCubic turns that
// first notch into 68% of the collapse. Scroll therefore sets a *target* and the
// rendered progress eases toward it, so the header glides instead of snapping.
- const EASE = 0.16, BLUR_STEP = 0.5;
+ const EASE = 0.16, BLUR_STEP = 0.5, FRAME = 1000 / 60; // EASE: the share of the distance covered in 1/60 s
- let RANGE = 320, raf = null, target = 0, current = 0;
+ let RANGE = 320, raf = null, target = 0, current = 0, last = 0; // last: time of the previous frame, 0 after a restart
function lerp(a, b, t) { return a + (b - a) * t; }
function clamp(v, lo, hi) { return Math.min(Math.max(v, lo), hi); }
@@ -253,16 +253,17 @@
set('rule', q(e, .01), v => coverRule.style.opacity = v);
}
- function tick() {
+ function tick(now) {
const d = target - current;
if (Math.abs(d) < 0.0005) { current = target; render(current); raf = null; return; }
- current += d * EASE;
+ const dt = last ? Math.min(now - last, 50) : FRAME; last = now; // the first frame after a restart counts as 1/60 s
+ current += d * (1 - Math.pow(1 - EASE, dt / FRAME)); // the same pace on a 30, 60 or 120Hz screen
render(current);
raf = requestAnimationFrame(tick);
}
function schedule() {
if (snaps()) { current = target; render(current); return; }
- if (raf === null) raf = requestAnimationFrame(tick);
+ if (raf === null) { last = 0; raf = requestAnimationFrame(tick); }
}
stage.addEventListener('scroll', () => {