I’d like to block the page scrolling but without change my page style (e.g. using overflow: hidden;).
I tried to use this:
JavaScript
x
6
1
$('body').on('scroll mousewheel touchmove', function(e) {
2
e.preventDefault()
3
e.stopPropagation()
4
return false
5
});
6
but but the scrolling didn’t blocked and I received this console error:
JavaScript
1
2
1
[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See <URL>
2
Any solutions?
Advertisement
Answer
I found out the right way to disable the scrolling:
JavaScript
1
6
1
function preventScroll(e) {
2
e.preventDefault()
3
}
4
5
document.addEventListener('wheel', preventScroll, { passive: false })
6
In order to allow it again:
JavaScript
1
2
1
document.removeEventListener('wheel', preventScroll, { passive: false })
2