When a user starts scrolling on a mobile device, can it be canceled with Javascript?

Viewed 35

When a user swipes a page on a mobile device and it starts scrolling with momentum, is it possible to cancel the scroll and its momentum with Javascript?

1 Answers

Add the style overflow: hidden to the container, and then remove it quickly. This will keep the scrolling position of the document, but stop all momentum.
(To cancel the scroll in this example, press any key, like a)

document.getElementById("myDiv").innerHTML += "A very tall div<br/>".repeat(300)


document.addEventListener("keypress", ()=>{
  document.body.style.overflow = "hidden"
  setTimeout(()=>
  document.body.style.overflow = "", 100)
})
<div id="myDiv"></div>

Related