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: Snap Scrolling shows lines where the box can stop and which stop it settles on (See inside) and No snapping beside Always snaps (Compare)

matin8e2c857a6175
diff --git a/animations/01-scroll-based/snap-scrolling/index.html b/animations/01-scroll-based/snap-scrolling/index.html
index 0ba47f69..f361d948 100644
--- a/animations/01-scroll-based/snap-scrolling/index.html
+++ b/animations/01-scroll-based/snap-scrolling/index.html
@@ -46,6 +46,42 @@
.dot{width:44px;height:44px;padding:0;border:0;background:none;font:inherit;display:grid;place-items:center;cursor:pointer}
.dot::before{content:'';width:8px;height:8px;border-radius:50%;background:var(--ui-muted);transition:background .2s,transform .2s}
.dot.active::before{background:var(--ui-accent);transform:scale(1.4)}
+ /* See inside and Compare. No mode changes a section's height or the box's height, so the stops stay where they are and the
+ browser has nothing to re-snap. Outside their modes the marks, the labels and the copy are not drawn. */
+ .tick,.say,.half,.copy{display:none}
+ .say,.half{position:absolute;left:10px;padding:4px 9px;border-radius:7px;background:rgba(11,11,13,.86);color:#f4f4f2;
+ font:600 13px/1.3 var(--hb-font);white-space:nowrap;pointer-events:none}
+ /* See inside: each section's top is a stop, drawn as a bright line on the seam (half on each side, so at rest the box shows
+ the stop it sits on at its top and the next one at its bottom; faint with Snapping off), and the sticky row marks the box's
+ top edge, which a stop lines up with. Always settles on the nearest stop, so a dashed line across each middle shows where
+ that flips; When close settles only within a third of the box of a stop (Chrome's reach, measured), so a band shades a
+ third of the box above and below each stop. Dashes and bands lie behind the words. */
+ .stage[data-hb-mode="inside"] .tick,.stage[data-hb-mode="inside"] .say{display:block}
+ .tick{position:absolute;left:0;top:0;width:44px;height:7px;border-radius:0 4px 4px 0;background:#f4f4f2}
+ .say-stop{top:14px}
+ .say-rule{top:calc(100cqh - 10px);translate:0 -100%;max-width:calc(100cqw - 66px);white-space:normal}
+ .stage[data-hb-mode="inside"] .section{position:relative;z-index:0;text-shadow:0 0 3px #0b0b0d,0 0 6px #0b0b0d}
+ .stage[data-hb-mode="inside"] .section::before{content:'';position:absolute;inset:0;border:solid var(--ui-accent);border-width:3px 0}
+ .stage[data-hb-mode="inside"] .section:last-child::before{border-bottom:0}
+ .stage[data-hb-mode="inside"] .scroller[data-snap="off"] .section::before{border-color:#6ea8ff59}
+ .stage[data-hb-mode="inside"] .scroller[data-snap="mandatory"] .section::after{content:'';position:absolute;inset:50% 0 auto;
+ z-index:-1;border-top:2px dashed var(--ui-accent)}
+ .stage[data-hb-mode="inside"] .scroller[data-snap="proximity"] .section::after{content:'';position:absolute;inset:0;z-index:-1;
+ background:linear-gradient(#6ea8ff3d 33.33%,transparent 0 66.67%,#6ea8ff3d 0)}
+ .stage[data-hb-mode="inside"] .scroller[data-snap="proximity"] .section:last-child::after{background:linear-gradient(#6ea8ff3d 33.33%,transparent 0)}
+ /* Compare: a copy that never snaps on the left, the page's own box on the right, side by side at every width, so only the
+ real box's width changes, never its height. In a half each section keeps its number and title. */
+ .stage[data-hb-mode="compare"]{display:flex;gap:3px}
+ .stage[data-hb-mode="compare"] .scroller{flex:1 1 0;min-width:0;overflow-x:hidden}
+ .stage[data-hb-mode="compare"] .copy,.stage[data-hb-mode="compare"] .half{display:block}
+ .half{top:10px}
+ .copy{scroll-behavior:auto}
+ .stage[data-hb-mode="compare"] .sec-sub,.stage[data-hb-mode="compare"] .stats,.stage[data-hb-mode="compare"] .quote-attr{display:none}
+ /* the words sit at the top of each section, under the half's name, so a box that rests between two sections shows the next
+ one's number and title partway down */
+ .stage[data-hb-mode="compare"] .section{justify-content:flex-start;padding:48px 52px 12px 12px}
+ .stage[data-hb-mode="compare"] .sec-title{font-size:clamp(16px,5.4cqw,26px);text-wrap:balance}
+ .stage[data-hb-mode="compare"] .pull-quote{font-size:clamp(13px,4.2cqw,20px);margin:0;text-wrap:balance}
/* Reduced motion: no smooth-scroll animation, no dot tweening */
@media(prefers-reduced-motion:reduce){.scroller{scroll-behavior:auto!important}.dot::before{transition:none!important}}
</style>
@@ -70,13 +106,22 @@
<span class="hb-num" aria-hidden="true">1</span>
<div><h2 id="hb-watch-title">Scroll it</h2><p>Scroll inside the box and let go: it settles on the nearest section. Press Play and it glides through without snapping.</p></div>
</div>
+ <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
+ <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
+ <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
+ </div>
<div class="stage">
- <div class="scroller" id="scroller" data-hb-scroller data-snap="mandatory" tabindex="0" role="region" aria-label="Five sections that the box snaps to"><div class="snap-rail"><div class="snap-dots" id="snap-dots"></div></div></div>
+ <div class="scroller" id="scroller" data-hb-scroller data-snap="mandatory" tabindex="0" role="region" aria-label="Five sections that the box snaps to"><div class="snap-rail"><div class="snap-dots" id="snap-dots"></div><i class="tick"></i><p class="say say-stop" data-hb-caption>A stop lines up here</p><p class="say say-rule" data-hb-caption>Past a dashed line, it settles on the next</p><p class="half" data-hb-caption>Always snaps</p></div></div>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>Play</button>
<button class="hb-play" type="button" id="btn-top" data-hb-top><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>Back to top</button>
</div>
+ <div class="hb-mode-notes" aria-live="polite">
+ <p data-hb-for="inside" hidden>Inside, solid lines mark where the box can stop, each section's top, and past a dashed middle line it settles on the next one. Scroll the box a little and let go.</p>
+ <p data-hb-for="compare" hidden>No snapping rests wherever you let go; Always snaps settles on the nearest whole section. Scroll either box a little and let go.</p>
+ </div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
@@ -168,7 +213,7 @@
pick(b.dataset);
}));
}
- choices('snap-seg',d=>{scroller.dataset.snap=d.snap});
+ choices('snap-seg',d=>{scroller.dataset.snap=d.snap;describe();if(mode==='compare')pose()});
const dots=snapDots.querySelectorAll('.dot');
function showSection(){
const idx=Math.min(Math.round(scroller.scrollTop/scroller.clientHeight),4);
@@ -179,6 +224,57 @@
}
scroller.addEventListener('scroll',showSection,{passive:true});
addEventListener('resize',showSection,{passive:true});
+
+ // Compare's left box: a copy of the five sections in a box of its own that never snaps, named "No snapping". It must take a
+ // scroll by hand, so it is not inert; it has no ids, no dots and no tab stop, and screen readers skip it.
+ const copy=document.createElement('div');
+ copy.className='scroller copy';copy.tabIndex=-1;copy.setAttribute('aria-hidden','true');
+ copy.innerHTML='<div class="snap-rail"><p class="half" data-hb-caption>No snapping</p></div>';
+ scroller.querySelectorAll('.section').forEach(s=>copy.appendChild(s.cloneNode(true)));
+ scroller.before(copy);
+ const ruleSay=scroller.querySelector('.say-rule'),half=scroller.querySelector('.half');
+ const insideNote=document.querySelector('[data-hb-for="inside"]'),compareNote=document.querySelector('[data-hb-for="compare"]');
+ // The words for each Snapping choice: See inside's label, the right box's name and the two notes
+ const WORDS={
+ mandatory:{rule:'Past a dashed line, it settles on the next',half:'Always snaps',
+ inside:"Inside, solid lines mark where the box can stop, each section's top, and past a dashed middle line it settles on the next one. Scroll the box a little and let go.",
+ compare:'No snapping rests wherever you let go; Always snaps settles on the nearest whole section. Scroll either box a little and let go.'},
+ proximity:{rule:'It settles only inside a shaded band',half:'Snaps when close',
+ inside:"Inside, solid lines mark the stops, and the box settles on one only if its top is in that stop's shaded band when you let go. Scroll the box a little and let go.",
+ compare:'No snapping rests wherever you let go; Snaps when close settles on a section only when you stop near one. Scroll either box a little and let go.'},
+ off:{rule:'Snapping is off: it rests anywhere',half:'Snapping off',
+ inside:'Inside, faint lines mark where the box could stop, but with Snapping off it rests wherever you let go. Scroll the box a little and let go.',
+ compare:'Snapping is set to Off, so No snapping and Snapping off both rest wherever you let go. Pick another Snapping in Try it, then scroll either box a little and let go.'}
+ };
+ const NAMES={normal:'Five sections that the box snaps to',inside:'Five sections that the box snaps to, with lines where it can stop',
+ compare:'Five sections that the box snaps to, beside a copy that does not snap'};
+ let mode='normal',following=0;
+ // Play marks the box while it scrolls it (and the shared stylesheet turns snapping off for that while)
+ const playing=()=>'hbAutoscrolling' in scroller.dataset;
+ function describe(){
+ const w=WORDS[scroller.dataset.snap];
+ ruleSay.textContent=w.rule;half.textContent=w.half;insideNote.textContent=w.inside;compareNote.textContent=w.compare;
+ }
+ // The left box is placed at once, four tenths of a section short of the right box (past it at the top), so at rest it shows
+ // the same section's words partway down, under the end of the one before, while the right one shows that section whole;
+ // level with it when snapping is off or Play is running
+ function pose(){
+ const H=scroller.clientHeight,top=scroller.scrollTop;
+ const apart=scroller.dataset.snap!=='off'&&!playing();
+ copy.scrollTo({top:apart?(top>=.4*H?top-.4*H:top+.4*H):top,behavior:'instant'});
+ }
+ document.addEventListener('hb:mode',e=>{
+ mode=e.detail.mode;
+ scroller.setAttribute('aria-label',NAMES[mode]);
+ if(mode==='compare')pose();
+ });
+ // While Play runs (snapping is off in both boxes then) the left box follows the right one, in the same frame
+ scroller.addEventListener('scroll',()=>{
+ if(mode!=='compare'||following||!playing())return;
+ following=requestAnimationFrame(()=>{following=0;if(mode==='compare')copy.scrollTo({top:scroller.scrollTop,behavior:'instant'})});
+ },{passive:true});
+ document.getElementById('btn-top').addEventListener('click',()=>copy.scrollTo({top:0,behavior:'instant'}));
+ describe();
</script>
</body>
</html>