jQuery 如何在智能手机和平板电脑浏览器中禁用滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10097937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:50:14  来源:igfitidea点击:

How to disable scrolling in smartphone and tablet browsers?

jqueryiphonescrollsmartphone

提问by Orb Hitter

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

我使用以下代码禁用桌面浏览器中的滚动,但它不适用于 iPhone 屏幕分辨率。

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

What else do I need to add?

我还需要添加什么?

回答by Fresheyeball

//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;
}

回答by CyberFox

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

我将提供一个不使用 jQuery 的部分,以便下一个“Javascripter”可以直接复制粘贴:

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);