简体   繁体   English

在所有移动设备中禁用滚动

[英]Disable scrolling in all mobile devices

This sounds like there should be a solution for it all over the internet, but I am not sure why I cannot find it.这听起来应该在整个互联网上都有解决方案,但我不确定为什么我找不到它。 I want to disable Horizontal scrolling on mobile devices.我想在移动设备上禁用水平滚动。 Basically trying to achieve this:基本上试图实现这一目标:

body{
   overflow-x:hidden  // disable horizontal scrolling.
}

This may be relevant information: I also have this in my head tag, because I also do not wish the user to be able to zoom:这可能是相关信息:我的头部标签中也有这个,因为我也不希望用户能够缩放:

<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />
<meta name="viewport" content="width=device-width" />

Thanks谢谢

html, body {
  overflow-x: hidden;
}
body {
  position: relative;
}

The position relative is important, and i just stumbled about it.相对位置很重要,我只是偶然发现了它。 Could not make it work without it.没有它就无法工作。

cgvector answer didn't work for me, but this did: cgvector 答案对我不起作用,但这样做了:

document.body.addEventListener('touchstart', function(e){ e.preventDefault(); });

I wouldn't leave it just like that, a smarter logic is needed to select when to prevent the scrolling, but this is a good start.我不会就这样离开它,需要一个更聪明的逻辑来选择何时阻止滚动,但这是一个好的开始。

Taken from here: Disable scrolling in an iPhone web application?取自此处: 禁用 iPhone Web 应用程序中的滚动?

For future generations:为后代:

To prevent scrolling but keep the contextmenu, try要防止滚动但保留上下文菜单,请尝试

document.body.addEventListener('touchmove', function(e){ e.preventDefault(); });

It still prevents way more than some might like, but for most browsers the only default behaviour prevented should be scrolling.它仍然阻止了一些人可能喜欢的方式,但对于大多数浏览器,唯一阻止的默认行为应该是滚动。

For a more sophisticated solution that allows for scrollable elements within the nonscrollable body and prevents rubberband, have a look at my answer over here:对于允许在不可滚动主体中使用可滚动元素并防止橡皮筋的更复杂的解决方案,请在此处查看我的答案:

https://stackoverflow.com/a/20250111/1431156 https://stackoverflow.com/a/20250111/1431156

I suspect most everyone really wants to disable zoom/scroll in order to put together a more app-like experience;我怀疑大多数人真的想禁用缩放/滚动,以便组合更像应用程序的体验; because the answers seem to contain elements of solutions for both zooming and scrolling, but nobody's really nailed either one down.因为答案似乎包含缩放和滚动解决方案的元素,但没有人真正确定其中任何一个。

Scrolling滚动

To answer OP, the only thing you seem to need to do to disable scrolling is intercept the window's scroll and touchmove events and call preventDefault and stopPropagation on the events they generate;要回答 OP,禁用滚动似乎唯一需要做的就是拦截窗口的scrolltouchmove事件,并在它们生成的事件上调用preventDefaultstopPropagation like so像这样

window.addEventListener("scroll", preventMotion, false);
window.addEventListener("touchmove", preventMotion, false);

function preventMotion(event)
{
    window.scrollTo(0, 0);
    event.preventDefault();
    event.stopPropagation();
}

And in your stylesheet, make sure your body and html tags include the following:在你的样式表中,确保你的bodyhtml标签包括以下内容:

html:
{
    overflow: hidden;
}

body
{
    overflow: hidden;
    position: relative;
    margin: 0;
    padding: 0;
}

Zooming缩放

However, scrolling is one thing, but you probably want to disable zoom as well.但是,滚动是一回事,但您可能还想禁用缩放。 Which you do with the meta tag in your markup:您对标记中的元标记执行的操作:

<meta name="viewport" content="user-scalable=no" />

All of these put together give you an app-like experience, probably a best fit for canvas.所有这些组合在一起为您提供类似应用程序的体验,可能最适合画布。

(Be wary of the advice of some to add attributes like initial-scale and width to the meta tag if you're using a canvas, because canvasses scale their contents, unlike block elements, and you'll wind up with an ugly canvas, more often than not). (如果您使用画布,请注意某些人的建议,即在元标记中添加诸如initial-scalewidth类的属性,因为画布会缩放其内容,与块元素不同,您最终会得到一个丑陋的画布,往往不是)。

Try adding尝试添加

html {
  overflow-x: hidden;
}

as well as

body {
  overflow-x: hidden;
}

use this in style用这个风格

body
{
overflow:hidden;
width:100%;
}

Use this in head tag在 head 标签中使用它

<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />

In page header, add在页眉中添加

<meta name="viewport" content="width=device-width, initial-scale=1, minimum-sacle=1, maximum-scale=1, user-scalable=no">

In page stylesheet, add在页面样式表中,添加

html, body {
  overflow-x: hidden;
  overflow-y: hidden;
}

It is both html and body!它既是 html 又是正文!

The CSS property touch-action may get you what you are looking for, though it may not work in all your target browsers. CSS 属性touch-action可能会得到您想要的东西,尽管它可能不适用于您的所有目标浏览器。

html, body {
    width: 100%; height: 100%;
    overflow: hidden;
    touch-action: none;
}

Setting position to relative does not work for me.位置设置为相对对我不起作用。 It only works if I set the position of body to fixed :仅当我将 body 的位置设置为fixed时才有效:

document.body.style.position = "fixed";
document.body.style.overflowY = "hidden";
document.body.addEventListener('touchstart', function(e){ e.preventDefault()});

This works for me:这对我有用:

window.addEventListener("touchstart", function(event){
             if(event.target.tagName=="HTML" || event.target.tagName=="BODY"){
                     event.preventDefault();
             }
} ,false);

It does not work 100% and I also added this:它不能 100% 工作,我还添加了以下内容:

window.addEventListener("scroll",function(){
    window.scrollTo(0,0)
},false)

After having no success trying all the answers I managed to turn my mobile scroll off by simply adding:在尝试所有答案都没有成功之后,我设法通过简单地添加以下内容来关闭我的移动滚动:

html,
body {
  overflow-x: hidden;
  height: 100%;
}

body {
  position: relative;
}

Its important to use % not vh for this.为此使用%而不是vh很重要。 The height: 100% was something I had been missing all along, crazy. height: 100%是我一直想念的东西,太疯狂了。

The simplest way which is pretty much straight forward with only CSS Codes:最简单的方法,仅使用 CSS代码就非常简单:

 body, html { overflow-x: hidden; position: relative; }

For Apple devices position: relative does not work.对于 Apple 设备position: relative不起作用。 The following code works on all devices:以下代码适用于所有设备:

html, body {
  overflow: hidden;
  position: fixed;
}

I'm late to the party, but I'm adding this answer because none of the other things I tried worked in my specific situation.我参加聚会迟到了,但我添加了这个答案,因为我尝试过的其他任何事情都没有在我的特定情况下起作用。 No combination of capturing touchmove, scroll etc events had any effect, and position: relative on body made everything disappear.捕获 touchmove、scroll 等事件的组合没有任何效果,并且 position: relative 在 body 上使一切都消失了。

I found that I had to add height: 100% on the HTML and BODY elements.我发现我必须在 HTML 和 BODY 元素上添加 height: 100% 。 It's possible that not all of the following rules are required, but it took me long enough to stumble upon this magic combination and it appears to work.可能并非所有以下规则都是必需的,但我花了足够长的时间偶然发现了这个神奇的组合,它似乎有效。

html {
    height: 100%;
    overflow: hidden;
}
body{
    margin: 0;
    overflow: hidden;
    height: 100%;
    position: relative;
}

In addition to this I also had the following in the HTML head:除此之外,我还在 HTML 头中有以下内容:

<meta name="viewport" content="width=device-width, initial-scale=1">

My app made heavy use of absolutely position elements, some of which were partially off-screen at different times as they slid out of view.我的应用程序大量使用了绝对位置元素,其中一些元素在不同时间滑出视图时部分离开屏幕。 As a result, the body element had no content in it to give the body any area/size.因此,body 元素中没有任何内容可以为 body 提供任何面积/大小。

I found that the "scrolling" that was happening on mobile devices was a result of the browser not knowing the extent of the page's size in order to do its viewport (scaling to device-width) calculation.我发现在移动设备上发生的“滚动”是由于浏览器不知道页面大小的范围以进行其视口(缩放到设备宽度)计算的结果。 It seems that the body having 0px height was failing to convey to the browser either the height or width of the page, so setting the height of body to 100% was key in the solution.似乎具有 0px 高度的 body 无法将页面的高度或宽度传达给浏览器,因此将 body 的高度设置为 100% 是解决方案的关键。

Edit: looking at the answers again after finding my solution, I realise the other answer by "Toms Codery" is essentially the same solution as mine, albeit his is only in the x-direction.编辑:找到我的解决方案后再次查看答案,我意识到“Toms Codery”的另一个答案与我的解决方案本质上是相同的,尽管他只是在 x 方向上。

The following works for me, although I did not test every single device there is to test :-)以下对我有用,虽然我没有测试每一个要测试的设备:-)

$('body, html').css('overflow-y', 'hidden');
$('html, body').animate({
    scrollTop:0
}, 0);

这样可以防止在移动设备上滚动,但不能单击/单击。

document.body.addEventListener('touchstart', function(e){ e.preventDefault(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM