Add scoped mouse wheel navigation for desktop carousels
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
/* Shared finite carousel: fitting groups and every overflow item center exactly. */
|
||||
(() => {
|
||||
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
|
||||
const finePointer = matchMedia('(hover: hover) and (pointer: fine)');
|
||||
// WheelEvent has no device type. Keep macOS pixel gestures entirely native.
|
||||
const applePointer = /Mac|iPhone|iPad|iPod/i.test(navigator.userAgentData?.platform || navigator.platform || '');
|
||||
function bind(root) {
|
||||
if (root.dataset.carouselReady) return;
|
||||
root.dataset.carouselReady = 'true';
|
||||
@@ -73,6 +76,32 @@
|
||||
track.addEventListener('dragstart', event => event.preventDefault());
|
||||
// Prevent legacy page smooth-scroll from converting horizontal trackpad input.
|
||||
track.addEventListener('wheel', event => { if(Math.abs(event.deltaX)>Math.abs(event.deltaY)) event.stopPropagation(); },{passive:true});
|
||||
if (!applePointer) {
|
||||
let lastStep = -Infinity, lastDirection = 0, continuousUntil = 0;
|
||||
track.addEventListener('wheel', event => {
|
||||
if (!finePointer.matches || fits || dragging || event.defaultPrevented || !event.cancelable ||
|
||||
event.ctrlKey || event.metaKey || event.altKey || event.shiftKey ||
|
||||
event.target.closest('input,textarea,select,[contenteditable="true"],[role="slider"]')) return;
|
||||
const mode = event.deltaMode, dy = event.deltaY, now = performance.now();
|
||||
if (!dy || !Number.isFinite(dy)) return;
|
||||
// Coarse vertical steps include fractional deltas from browser scaling.
|
||||
// Fine/diagonal samples mark a continuous gesture, including its tail.
|
||||
const discrete = !event.deltaX && (mode === 1 || mode === 2 ||
|
||||
(mode === 0 && Math.abs(dy) >= 40));
|
||||
if (!discrete) { continuousUntil = now + 250; return; }
|
||||
if (now < continuousUntil) return;
|
||||
const direction = Math.sign(dy);
|
||||
// Consume a short burst while moving, but allow reversal immediately.
|
||||
if (now - lastStep < 220 && direction === lastDirection) {
|
||||
event.preventDefault(); event.stopPropagation(); return;
|
||||
}
|
||||
const target = active + direction;
|
||||
if (target < 0 || target >= items.length) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
lastStep = now; lastDirection = direction;
|
||||
go(target);
|
||||
}, {passive:false});
|
||||
}
|
||||
new ResizeObserver(layout).observe(root);
|
||||
const observer=new IntersectionObserver(entries=>entries.forEach(entry=>{if(!entry.isIntersecting)root.querySelectorAll('video').forEach(v=>v.pause());}));observer.observe(root);
|
||||
layout();
|
||||
|
||||
Reference in New Issue
Block a user