How to disable scrolling in smartphone and tablet browsers?
Asked Answered
S

2

13

I use the following code to disable scrolling in desktop browsers but it doesn't work for iPhone screen resolution.

$("html").css("overflow", "hidden");

What else do I need to add?

Scythia answered 10/4, 2012 at 23:33 Comment(0)
I
29
//target the entire page, and listen for touch events
$('html, body').on('touchstart touchmove', function(e){ 
     //prevent native touch activity like scrolling
     e.preventDefault(); 
});

if having the touch event blocked doesn't work for you, you can always go like this:

html, body{
     max-width:100%;
     max-height:100%;
     overflow:hidden;
}
Intertwist answered 10/4, 2012 at 23:35 Comment(3)
Thanks, your jQuery code worked nicely for what I was trying to do.Scythia
You have to remove the touchstart because with it you disable also the "click touch" eventTantrum
@MarcoMantovani that was not a requirement.Intertwist
V
5

I'm gonna provide an a piece that doesn't utilize jQuery so the next "Javascripter" can just copy'n'paste:

var defaultPrevent=function(e){e.preventDefault();}
document.body.parentElement.addEventListener("touchstart", defaultPrevent);
document.body.parentElement.addEventListener("touchmove" , defaultPrevent);
document.body.addEventListener("touchstart", defaultPrevent);
document.body.addEventListener("touchmove" , defaultPrevent);
Volkslied answered 22/12, 2013 at 18:46 Comment(2)
Just tested this on Android. defaultPreventDefault() doesn't seem to exist, but preventDefault() does work.Rigel
so much for copy pasta. It's a typo. And corrected now thanks.Volkslied

© 2022 - 2024 — McMap. All rights reserved.