This jsFiddle shows the issue.
I am trying to allow horizontal scroll snapping using scroll-snap-type
so that when the user scrolls, the scroll snaps to the nearest div.
I also want to scroll the div when the user presses a button. However, this doesn't seem to work when scroll-snap-type
is applied to the container.
As shown in the jsFiddle, removing scroll-snap-type
makes the button work. Keeping it, makes the button do nothing when clicked.
function myFunction() {
const element = document.getElementById("myDIV");
element.scrollLeft = element.scrollLeft + 50;
}
document.getElementById("button").addEventListener('click', function(){
myFunction();
})
CSS
.x.mandatory-scroll-snapping {
scroll-snap-type: x mandatory;
}
HTML
<button id="button"> CLICK</button>
<div class="container x mandatory-scroll-snapping" id="myDIV">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
You are not increasing the scroll position enough to snap to the next
<div>