I want to disable and replace the vertical scroll with horizontal one in Vanilla JavaScript. I am blocked when I try to replace my scroll values. I trust there is a simple simple (really simple) solution to that !
/* We define our function */
function replaceVerticalScrollByHorizontal() {
let scrollX = window.scrollX; /* Horizontal ➡️ */
let scrollY = window.scrollY; /* Vertical ⬇️ */
console.log({scrollX, scrollY});
if (scrollY !== 0) {
/* If there is a changement in verticalScroll ⬇️ */
const tempScrollY = scrollY; /* We create a temporary var to stock our scrollY value */
scrollX = scrollY; /* Horizontal = Vertical */
scrollY = 0; /* We reset the vertical because we don't want any vertical scroll */
window.scrollTo(scrollX, scrollY); /* We scroll to our value */
scrollY = tempScrollY; /* We replace the value of our vertical var with the temporary one */
}
/* If no verticalScroll changement, exit of this function ⬇️ */
return;
}
/* Listener on window once we start scrolling, we run our function */
window.addEventListener('scroll', replaceVerticalScrollByHorizontal);
There is a little jsfiddle with my problem: https://jsfiddle.net/zynj64m7/2/