Create a button in your HTML to toggle scroll snapping:
<button id="toggleScrollSnapping">Toggle Scroll Snapping</button>
Add an event listener to the button to toggle the scroll snapping behavior:
const toggleButton = document.getElementById('toggleScrollSnapping');
const htmlElement = document.documentElement;
toggleButton.addEventListener('click', () => {
htmlElement.classList.toggle('scroll-snapping-disabled');
});
Define your CSS rules to enable or disable scroll snapping based on the class applied to the element:
html {
/* ... other CSS rules ... */
}
body {
margin: 0;
flex-direction: column;
}
.scroll-snapping-enabled {
scroll-snap-type: y mandatory;
}
.scroll-snapping-disabled {
scroll-snap-type: none;
}
Apply the CSS class to the element when the button is clicked:
toggleButton.addEventListener('click', () => {
htmlElement.classList.toggle('scroll-snapping-disabled');
htmlElement.classList.toggle('scroll-snapping-enabled');
});