When enabling CSS scroll snap, it stops a Javascript wheel event listener from working correctly.
I have created a horizontal website that uses a wheel event listener to scroll on the X axis when scrolling with a mouse wheel.
As demonstrated below:
const scrollContainer = document.querySelector('.container');
scrollContainer.addEventListener('wheel', (e) => {
e.preventDefault();
scrollContainer.scrollLeft += e.deltaY;
});
* {
margin: 0;
}
.container {
display: flex;
overflow-x: scroll;
/*scroll-snap-type: x mandatory;*/
}
section {
height: 100vh;
width: 300vw;
min-width: 100vw;
/*scroll-snap-align: start;*/
}
#section1 {
background: red;
}
#section2 {
background: yellow;
}
#section3 {
background: blue;
}
<html>
<div class="container">
<section id="section1"></section>
<section id="section2"></section>
<section id="section3"></section>
</div>
</html>
When i add the following code to enable scroll snap:
.container {
scroll-snap-type: x mandatory;
}
section {
scroll-snap-align: start;
}
It stops the wheel event listener from functioning correctly, scroll snap doesn't work either.
e.preventDefault();
The preventDefault is the reason it is not working, but i am yet to find away around this problem.