Align the page to nearby carousels before wheel slide navigation
This commit is contained in:
@@ -14,7 +14,37 @@
|
||||
const count = root.querySelector('[data-carousel-count]');
|
||||
let active = 0, fits = false, settle, dragging = null, layoutWidth = 0;
|
||||
let wheelFrame = 0, wheelSnap = '';
|
||||
let pageFrame = 0, alignmentQuietUntil = 0, alignmentDirection = 0;
|
||||
const center = item => item.offsetLeft + item.offsetWidth / 2 - track.clientWidth / 2;
|
||||
function cancelPageInput(event) {
|
||||
if (event.type !== 'wheel' || !track.contains(event.target)) stopPageMotion();
|
||||
}
|
||||
function stopPageMotion() {
|
||||
if (pageFrame) cancelAnimationFrame(pageFrame);
|
||||
pageFrame = 0;
|
||||
window.removeEventListener('wheel', cancelPageInput, true);
|
||||
window.removeEventListener('pointerdown', cancelPageInput, true);
|
||||
window.removeEventListener('keydown', cancelPageInput, true);
|
||||
}
|
||||
function alignPage(offset, direction) {
|
||||
const from = window.scrollY, start = performance.now();
|
||||
const max = Math.max(0, document.documentElement.scrollHeight - window.innerHeight);
|
||||
const destination = Math.max(0, Math.min(max, from + offset));
|
||||
if (Math.abs(destination - from) < 1) return false;
|
||||
stopWheelMotion();
|
||||
alignmentDirection = direction;
|
||||
window.addEventListener('wheel', cancelPageInput, {capture:true, passive:true});
|
||||
window.addEventListener('pointerdown', cancelPageInput, {capture:true, passive:true});
|
||||
window.addEventListener('keydown', cancelPageInput, {capture:true, passive:true});
|
||||
const frame = now => {
|
||||
const progress = Math.min(1, Math.max(0, (now - start) / 300));
|
||||
window.scrollTo({top:from + (destination - from) * (1 - Math.pow(1 - progress, 3)), behavior:'instant'});
|
||||
if (progress < 1) pageFrame = requestAnimationFrame(frame);
|
||||
else { stopPageMotion(); alignmentQuietUntil = now + 180; }
|
||||
};
|
||||
pageFrame = requestAnimationFrame(frame);
|
||||
return true;
|
||||
}
|
||||
function stopWheelMotion() {
|
||||
if (!wheelFrame) return;
|
||||
cancelAnimationFrame(wheelFrame);
|
||||
@@ -45,6 +75,7 @@
|
||||
});
|
||||
}
|
||||
function go(index, smooth = true, wheelMotion = false) {
|
||||
stopPageMotion();
|
||||
stopWheelMotion();
|
||||
active = Math.max(0, Math.min(items.length - 1, index));
|
||||
const left = fits ? 0 : center(items[active]);
|
||||
@@ -82,6 +113,7 @@
|
||||
}, {passive:true});
|
||||
// Touch uses native horizontal scrolling; vertical gestures continue down the page.
|
||||
track.addEventListener('pointerdown', event => {
|
||||
stopPageMotion();
|
||||
stopWheelMotion();
|
||||
if (event.pointerType !== 'mouse' || fits || event.target.closest('video,button')) return;
|
||||
dragging = {x:event.clientX, left:track.scrollLeft, moved:false};
|
||||
@@ -112,25 +144,42 @@
|
||||
// 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) { stopWheelMotion(); continuousUntil = now + 250; return; }
|
||||
if (!discrete) { stopPageMotion(); stopWheelMotion(); continuousUntil = now + 250; return; }
|
||||
if (now < continuousUntil) return;
|
||||
const direction = Math.sign(dy);
|
||||
// The arrival gesture aligns the page only; it must not spend its tail
|
||||
// on horizontal slides. Reversing cancels alignment and releases the page.
|
||||
if (pageFrame || now < alignmentQuietUntil) {
|
||||
if (direction !== alignmentDirection) {
|
||||
stopPageMotion(); alignmentQuietUntil = 0; lastPageWheel = now; return;
|
||||
}
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
alignmentQuietUntil = now + 180;
|
||||
return;
|
||||
}
|
||||
const target = active + direction;
|
||||
if (target < 0 || target >= items.length) 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;
|
||||
const offset = (rect.top + rect.bottom - top - bottom) / 2;
|
||||
const distance = Math.abs(offset);
|
||||
const near = distance <= Math.min(240, (bottom - top) * 0.3) &&
|
||||
visible >= Math.min(rect.height, bottom - top) * 0.65;
|
||||
if (near && distance > 8 && (offset * direction > 0 || distance <= 80) && alignPage(offset, direction)) {
|
||||
event.preventDefault(); event.stopPropagation(); return;
|
||||
}
|
||||
const centered = distance <= 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) {
|
||||
event.preventDefault(); event.stopPropagation(); return;
|
||||
}
|
||||
const target = active + direction;
|
||||
if (target < 0 || target >= items.length) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
lastStep = now; lastDirection = direction;
|
||||
// User-requested wheel navigation remains smooth even with reduced
|
||||
|
||||
Reference in New Issue
Block a user