if (newScrollLeft >= 0 && newScrollLeft <= (elem.scrollWidth - elem.clientWidth)) {
elem.scrollLeft = newScrollLeft;
}
}, { passive: true });
}
else {
elem.style.overflowX = 'scroll';
}
const smoothScrollStep = () => {
const currentScrollLeft = elem.scrollLeft;
const distance = targetScrollLeft - currentScrollLeft;
if (Math.abs(distance) < 1) {
elem.scrollLeft = targetScrollLeft;
animationFrameId = null;
return;
}
const newScrollLeft = currentScrollLeft + distance * 0.1;
if (newScrollLeft >= 0 && newScrollLeft <= (elem.scrollWidth - elem.clientWidth)) {
elem.scrollLeft = newScrollLeft;
}
animationFrameId = requestAnimationFrame(smoothScrollStep);
};
const stopRunningAnimation = () => {
if (animationFrameId !== null) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
elem.style.setProperty('scroll-behavior', 'auto', 'important');
elem.scrollLeft = elem.scrollLeft;
elem.style.scrollBehavior = '';
};
const handleWheelScroll = (event) => {
if (!isElementCenteredInViewport(elem)) {
return;
}
const visibleWidth = elem.offsetWidth;
const maxScrollLeft = elem.scrollWidth - visibleWidth;
if (maxScrollLeft <= 0) {
return;
}
const delta = event.deltaY > 0 ? 1 : -1;
const nextPosition = elem.scrollLeft + delta * Math.max(20, Math.round(visibleWidth / 20));
const atLeftBoundary = elem.scrollLeft - 20 <= 0.5 && delta < 0;
const atRightBoundary = elem.scrollLeft + 20 >= (maxScrollLeft - 0.5) && delta > 0;
if (atLeftBoundary || atRightBoundary) return;
event.preventDefault();
targetScrollLeft = Math.min(Math.max(0, nextPosition), maxScrollLeft);
if (!animationFrameId) {
animationFrameId = requestAnimationFrame(smoothScrollStep);
}
};
elem.addEventListener('wheel', handleWheelScroll, { passive: false });
});
});
if (newScrollLeft >= 0 && newScrollLeft <= (elem.scrollWidth - elem.clientWidth)) {
elem.scrollLeft = newScrollLeft;
}
}, { passive: true });
}
else {
elem.style.overflowX = 'scroll';
}
const smoothScrollStep = () => {
const currentScrollLeft = elem.scrollLeft;
const distance = targetScrollLeft - currentScrollLeft;
if (Math.abs(distance) < 1) {
elem.scrollLeft = targetScrollLeft;
animationFrameId = null;
return;
}
const newScrollLeft = currentScrollLeft + distance * 0.1;
if (newScrollLeft >= 0 && newScrollLeft <= (elem.scrollWidth - elem.clientWidth)) {
elem.scrollLeft = newScrollLeft;
}
animationFrameId = requestAnimationFrame(smoothScrollStep);
};
const stopRunningAnimation = () => {
if (animationFrameId !== null) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
elem.style.setProperty('scroll-behavior', 'auto', 'important');
elem.scrollLeft = elem.scrollLeft;
elem.style.scrollBehavior = '';
};
const handleWheelScroll = (event) => {
if (!isElementCenteredInViewport(elem)) {
return;
}
const visibleWidth = elem.offsetWidth;
const maxScrollLeft = elem.scrollWidth - visibleWidth;
if (maxScrollLeft <= 0) {
return;
}
const delta = event.deltaY > 0 ? 1 : -1;
const nextPosition = elem.scrollLeft + delta * Math.max(20, Math.round(visibleWidth / 10));
const atLeftBoundary = elem.scrollLeft - 20 <= 0.5 && delta < 0;
const atRightBoundary = elem.scrollLeft + 20 >= (maxScrollLeft - 0.5) && delta > 0;
event.preventDefault();
targetScrollLeft = Math.min(Math.max(0, nextPosition), maxScrollLeft);
if (!animationFrameId) {
animationFrameId = requestAnimationFrame(smoothScrollStep);
}
};
elem.addEventListener('wheel', handleWheelScroll, { passive: false });
});
});