Avoid early carousel wheel capture and smooth explicit wheel navigation

This commit is contained in:
2026-11-12 18:00:57 +03:00
parent f24c881a3a
commit 6cac9a1704
5 changed files with 94 additions and 14 deletions
+42 -6
View File
@@ -13,7 +13,27 @@
const next = root.querySelector('[data-carousel-next]');
const count = root.querySelector('[data-carousel-count]');
let active = 0, fits = false, settle, dragging = null, layoutWidth = 0;
let wheelFrame = 0, wheelSnap = '';
const center = item => item.offsetLeft + item.offsetWidth / 2 - track.clientWidth / 2;
function stopWheelMotion() {
if (!wheelFrame) return;
cancelAnimationFrame(wheelFrame);
wheelFrame = 0;
track.style.scrollSnapType = wheelSnap;
}
function moveWithWheel(left) {
const from = track.scrollLeft, start = performance.now();
wheelSnap = track.style.scrollSnapType;
track.style.scrollSnapType = 'none';
const frame = now => {
const progress = Math.min(1, Math.max(0, (now - start) / 300));
const eased = 1 - Math.pow(1 - progress, 3);
track.scrollTo({left: from + (left - from) * eased, behavior:'instant'});
if (progress < 1) wheelFrame = requestAnimationFrame(frame);
else { wheelFrame = 0; track.style.scrollSnapType = wheelSnap; }
};
wheelFrame = requestAnimationFrame(frame);
}
function state() {
prev.disabled = fits || active === 0;
next.disabled = fits || active === items.length - 1;
@@ -24,9 +44,12 @@
if (i !== active && !fits) item.querySelectorAll('video').forEach(v => v.pause());
});
}
function go(index, smooth = true) {
function go(index, smooth = true, wheelMotion = false) {
stopWheelMotion();
active = Math.max(0, Math.min(items.length - 1, index));
track.scrollTo({left: fits ? 0 : center(items[active]), behavior: smooth && !reduced.matches ? 'smooth' : 'instant'});
const left = fits ? 0 : center(items[active]);
if (wheelMotion && smooth && !fits) moveWithWheel(left);
else track.scrollTo({left, behavior: smooth && !reduced.matches ? 'smooth' : 'instant'});
state();
}
function layout() {
@@ -52,13 +75,14 @@
track.addEventListener('scroll', () => {
clearTimeout(settle);
settle = setTimeout(() => {
if (fits || dragging) return;
if (fits || dragging || wheelFrame) return;
active = items.reduce((best, item, i) => Math.abs(center(item)-track.scrollLeft) < Math.abs(center(items[best])-track.scrollLeft) ? i : best, 0);
state();
}, 100);
}, {passive:true});
// Touch uses native horizontal scrolling; vertical gestures continue down the page.
track.addEventListener('pointerdown', event => {
stopWheelMotion();
if (event.pointerType !== 'mouse' || fits || event.target.closest('video,button')) return;
dragging = {x:event.clientX, left:track.scrollLeft, moved:false};
});
@@ -77,7 +101,7 @@
// 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;
let lastStep = -Infinity, lastDirection = 0, continuousUntil = 0, lastPageWheel = -Infinity;
track.addEventListener('wheel', event => {
if (!finePointer.matches || fits || dragging || event.defaultPrevented || !event.cancelable ||
event.ctrlKey || event.metaKey || event.altKey || event.shiftKey ||
@@ -88,8 +112,18 @@
// 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 (!discrete) { stopWheelMotion(); continuousUntil = now + 250; return; }
if (now < continuousUntil) return;
// Hover is not enough: first let the reader bring the media into view.
const media = items[active].querySelector('.carousel-media') || items[active];
const rect = media.getBoundingClientRect();
const top = 64, bottom = window.innerHeight - 32;
const visible = Math.max(0, Math.min(rect.bottom, bottom) - Math.max(rect.top, top));
const centered = Math.abs((rect.top + rect.bottom - top - bottom) / 2) <= 80;
if (!centered || visible < Math.min(rect.height, bottom - top) * 0.95 || now - lastPageWheel < 180) {
lastPageWheel = now;
return;
}
const direction = Math.sign(dy);
// Consume a short burst while moving, but allow reversal immediately.
if (now - lastStep < 220 && direction === lastDirection) {
@@ -99,7 +133,9 @@
if (target < 0 || target >= items.length) return;
event.preventDefault(); event.stopPropagation();
lastStep = now; lastDirection = direction;
go(target);
// User-requested wheel navigation remains smooth even with reduced
// decorative motion. Native gestures and other controls retain their policy.
go(target, true, true);
}, {passive:false});
}
new ResizeObserver(layout).observe(root);