2023-04-28 21:20:56 +02:00
|
|
|
const scrollDivHorizontally = (id) => {
|
|
|
|
var scrollDelta = 0; // variable to store the accumulated scroll delta
|
|
|
|
var isScrolling = false; // variable to track if scroll is already in progress
|
2023-04-29 12:43:12 +02:00
|
|
|
const divToScrollHorizontally = document.getElementById(id)
|
2023-04-28 21:20:56 +02:00
|
|
|
function scrollLoop() {
|
|
|
|
// Scroll the div horizontally by a fraction of the accumulated scroll delta
|
2023-04-29 12:43:12 +02:00
|
|
|
divToScrollHorizontally.scrollLeft += scrollDelta * 0.1;
|
2023-04-28 21:20:56 +02:00
|
|
|
|
|
|
|
// Reduce the accumulated scroll delta by a fraction
|
|
|
|
scrollDelta *= 0.9;
|
|
|
|
|
|
|
|
// If scroll delta is still significant, continue the scroll loop
|
|
|
|
if (Math.abs(scrollDelta) > 0.1) {
|
|
|
|
requestAnimationFrame(scrollLoop);
|
|
|
|
} else {
|
|
|
|
isScrolling = false; // Reset scroll in progress flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
divToScrollHorizontally.addEventListener("wheel", function(e) {
|
|
|
|
e.preventDefault(); // prevent default mousewheel behavior
|
|
|
|
|
|
|
|
// Accumulate the horizontal scroll delta
|
|
|
|
scrollDelta -= e.deltaX || e.wheelDeltaX || -e.deltaY || -e.wheelDeltaY;
|
|
|
|
|
|
|
|
// If scroll is not already in progress, start the scroll loop
|
|
|
|
if (!isScrolling) {
|
|
|
|
isScrolling = true;
|
|
|
|
requestAnimationFrame(scrollLoop);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default scrollDivHorizontally;
|