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.

feat: support scroll demos with Play and Back to top

matinee76b24a6aba
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.js
index 989267c4..207f97e1 100644
--- a/assets/js/demo-page.js
+++ b/assets/js/demo-page.js
@@ -4,6 +4,7 @@
* Pause and CSS slow motion on loop pages, presses Show me on do-it pages and tells
* the page when the visitor takes over ("hb:input"), and follows reduced motion
* (Loop and Slow motion greyed out, loops start paused).
+ * On scroll pages Play scrolls the box, Back to top jumps to its top and the visitor's own input stops it.
* The pure helpers are exported for tests/demo-page.test.js. */
(function (root, factory) {
var api = factory();
@@ -186,6 +187,9 @@
var stage = page.querySelector('.stage');
var player = page.querySelector('.hb-player');
var demoCtl = page.querySelector('[data-hb-demo]');
+ var scrollCtl = page.querySelector('[data-hb-autoscroll]');
+ var topCtl = page.querySelector('[data-hb-top]');
+ var scroller = page.querySelector('[data-hb-scroller]') || stage;
var reduce = win.matchMedia ? win.matchMedia('(prefers-reduced-motion: reduce)') : null;
var promptText = text(promptEl);
@@ -356,6 +360,60 @@
});
}
+ // Scroll pages: Play scrolls the box from where it is to its end at a steady speed (the whole box in about six
+ // seconds), from the top when it is already at the end. The visitor's own wheel, touch, press or key input anywhere
+ // in the stage stops it, and so does Back to top, which jumps the box to the top.
+ var FULL_SCROLL_MS = 6000;
+ var scrollFrame = 0;
+ var snapKept = null;
+ // While the scroll runs, CSS scroll snapping is off on the scroller (its own inline value is kept), or every step
+ // would snap and the box would jump from one snap point to the next. It goes back on when the scroll ends, is
+ // stopped or Back to top is pressed, and the browser then settles on the nearest snap point by itself.
+ function snapOff() {
+ if (snapKept !== null || win.getComputedStyle(scroller).scrollSnapType === 'none') return;
+ snapKept = scroller.style.scrollSnapType;
+ scroller.style.scrollSnapType = 'none';
+ }
+ function snapOn() {
+ if (snapKept === null) return;
+ scroller.style.scrollSnapType = snapKept;
+ snapKept = null;
+ }
+ function cancelFrame() {
+ if (scrollFrame) win.cancelAnimationFrame(scrollFrame);
+ scrollFrame = 0;
+ }
+ function stopScroll() {
+ cancelFrame();
+ snapOn();
+ }
+ function scrollBox(top) { scroller.scrollTo({ top: top, behavior: 'instant' }); }
+ function autoscroll() {
+ cancelFrame(); // not stopScroll(): pressing Play during a run keeps snapping off, so the box does not snap in between
+ var end = scroller.scrollHeight - scroller.clientHeight;
+ if (end <= 0) { snapOn(); return; }
+ snapOff();
+ if (scroller.scrollTop >= end - 2) scrollBox(0);
+ var pos = scroller.scrollTop, last = 0, speed = end / FULL_SCROLL_MS;
+ function step(now) {
+ if (last) pos = Math.min(end, pos + (now - last) * speed);
+ last = now;
+ scrollBox(pos);
+ scrollFrame = pos < end ? win.requestAnimationFrame(step) : 0;
+ if (!scrollFrame) snapOn();
+ }
+ scrollFrame = win.requestAnimationFrame(step);
+ }
+ function setUpScroll() {
+ scrollCtl.addEventListener('click', autoscroll);
+ if (topCtl) topCtl.addEventListener('click', function () { stopScroll(); scrollBox(0); });
+ // On the stage, not the scroller: a menu or button beside an inner scroller is the visitor's input too.
+ var heard = stage || scroller;
+ ['wheel', 'touchstart', 'pointerdown', 'keydown'].forEach(function (type) {
+ heard.addEventListener(type, function (e) { if (e.isTrusted) stopScroll(); }, { passive: true });
+ });
+ }
+
// While the device asks for reduced motion, Loop and Slow motion are switched off and cannot be switched on, a
// loop starts paused, and a note in the player bar says why. Replay and Play still work.
var playerSwitches = [loopCtl, slowCtl].filter(Boolean);
@@ -387,6 +445,7 @@
if (slowCtl) slowCtl.addEventListener('change', replay);
if (pauseCtl) pauseCtl.addEventListener('click', function () { setPaused(!paused); });
if (demoCtl && stage) setUpVisitorInput();
+ if (scrollCtl && scroller) setUpScroll();
if (slowCtl && stage && stage.getAnimations && slowCtl.getAttribute('data-hb-slowmo') === 'css') {
slowCtl.addEventListener('change', function () { if (!slowing) slowStage(); });
if (slowCtl.checked) slowStage();
@@ -397,6 +456,7 @@
if (doc.body.hasAttribute('data-hb-autoplay')) {
win.setTimeout(function () {
if (demoCtl) { if (!(reduce && reduce.matches)) demoCtl.click(); return; }
+ if (scrollCtl) { if (!(reduce && reduce.matches)) scrollCtl.click(); return; }
if (loopCtl && !(reduce && reduce.matches)) {
// After Back or a reload a browser can bring Loop back already on; press Replay so the demo still starts.
if (loopCtl.checked) replay();
diff --git a/tests/pages.test.js b/tests/pages.test.js
index 07f8626b..fc4eaa5c 100644
--- a/tests/pages.test.js
+++ b/tests/pages.test.js
@@ -110,7 +110,17 @@ for (const d of steps) {
assert.equal(count(html, other), 0, `a do-it page has no ${other}`);
}
}
- for (const marker of ['data-hb-loop', 'data-hb-slowmo', 'data-hb-pause', 'data-hb-demo', 'data-hb-reset']) {
+ if (kind === 'scroll') {
+ assert.equal(count(html, 'data-hb-autoscroll'), 1, 'one Play control');
+ assert.equal(count(html, 'data-hb-top'), 1, 'one Back to top control');
+ assert.ok(player.includes('id="btn-scroll" data-hb-autoscroll') && player.includes('id="btn-top" data-hb-top'),
+ 'Play and Back to top are in the player bar');
+ assert.ok(count(html, 'data-hb-scroller') <= 1, 'at most one scroller');
+ for (const other of ['data-hb-replay', 'data-hb-loop', 'data-hb-pause', 'data-hb-demo']) {
+ assert.equal(count(html, other), 0, `a scroll page has no ${other}`);
+ }
+ }
+ for (const marker of ['data-hb-loop', 'data-hb-slowmo', 'data-hb-pause', 'data-hb-demo', 'data-hb-reset', 'data-hb-autoscroll', 'data-hb-top']) {
assert.ok(count(html, marker) <= 1, `at most one ${marker}`);
assert.equal(count(player, marker), count(html, marker), `${marker} is in the player bar`);
const input = (html.match(new RegExp(`<input[^>]*${marker}[^>]*>`)) || [''])[0];
diff --git a/tools/check-pages.mjs b/tools/check-pages.mjs
index 1d22f593..c59bd186 100644
--- a/tools/check-pages.mjs
+++ b/tools/check-pages.mjs
@@ -11,8 +11,10 @@
// it and Play starts it again (twice over), and the reduced-motion run checks that the loop starts paused. On do-it
// pages the desktop and reduced-motion runs press Show me and check that it visibly moves the stage (the desktop run
// waits for the run on arrival to end first), and the reduced-motion run checks that nothing moves before it is
-// pressed. The 320px phone runs the page checks (overflow, small targets, chips, README) and takes screenshots, and
-// nothing more.
+// pressed. On scroll pages the desktop and reduced-motion runs check that the box scrolls by itself on arrival (not
+// under reduced motion), that Back to top returns it to the top and stops it, that Play scrolls it and that a real
+// wheel turn over the box stops Play. The 320px phone runs the page checks (overflow, small targets, chips, README)
+// and takes screenshots, and nothing more.
// Exit code 1 on any problem.
import { spawn } from 'node:child_process';
import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
@@ -262,6 +264,30 @@ async function demoProblems(reduced) {
return problems;
}
+// Scroll pages: the box scrolls by itself on arrival (not under reduced motion); Back to top returns it to the top and
+// stops it; Play scrolls it; a real wheel turn over the box stops Play.
+async function scrollProblems(reduced) {
+ const box = `(document.querySelector('[data-hb-scroller]') || document.querySelector('.hb-page .stage'))`;
+ const pos = () => evaluate(`${box}.scrollTop`);
+ const problems = [];
+ const arrived = await pos();
+ if (reduced && arrived > 0) problems.push('the box scrolls by itself under reduced motion');
+ if (!reduced && arrived <= 0) problems.push('the box does not scroll by itself on arrival');
+ await evaluate(`document.querySelector('[data-hb-top]').click()`);
+ await sleep(500);
+ if (await pos() !== 0) problems.push('Back to top does not return the box to the top and stop it');
+ await evaluate(`document.querySelector('[data-hb-autoscroll]').click()`);
+ await sleep(800);
+ if (await pos() <= 0) problems.push('Play does not scroll the box');
+ const c = await evaluate(`(() => { const b = ${box}.getBoundingClientRect(); return { x: b.left + b.width / 2, y: b.top + Math.min(b.height, innerHeight - b.top) / 2 }; })()`);
+ await send('Input.dispatchMouseEvent', { type: 'mouseWheel', x: c.x, y: c.y, deltaX: 0, deltaY: 40 });
+ await sleep(300);
+ const held = await pos();
+ await sleep(600);
+ if (await pos() !== held) problems.push('turning the wheel does not stop Play');
+ return problems;
+}
+
let failures = 0;
try {
ws = new WebSocket(await pageSocket());
@@ -328,6 +354,7 @@ try {
if (setup.moves && !setup.reduce && result.kind === 'once') problems.push(...await movementProblems());
if (result.kind === 'loop' && (setup.moves || setup.reduce)) problems.push(...await loopProblems(!!setup.reduce));
if (result.kind === 'do' && (setup.moves || setup.reduce)) problems.push(...await demoProblems(!!setup.reduce));
+ if (result.kind === 'scroll' && (setup.moves || setup.reduce)) problems.push(...await scrollProblems(!!setup.reduce));
} catch (err) {
problems.push(`check failed: ${err.message}`);
}