简体   繁体   English

如何禁用网页上的水平滚动?

[英]How do you disable horizontal scrolling on a webpage?

How do you disable horizontal scrolling on a webpage? 如何禁用网页上的水平滚动?

I understand that this question has been asked many times before on stackoverflow ( here, for example ). 我知道在stackoverflow之前已经多次询问过这个问题( 例如,这里 )。

The most common answer says use CSS to set overflow-x: hidden; 最常见的答案是使用CSS设置overflow-x: hidden; or max-width:100% for the html/body elements. 或者max-width:100% html / body元素的max-width:100% However, these seem to hide the scrollbar but still allow the user to scroll with middle clicks, trackpad swiping, and touchscreen swiping. 但是,这些似乎隐藏了滚动条,但仍然允许用户通过中间点击,触控板滑动和触摸屏滑动进行滚动。 I'm looking for a solution that allows NO horizontal scrolling of any form. 我正在寻找一种不允许任何形式的水平滚动的解决方案。

The next most common answer says don't make your content wider than the screen. 下一个最常见的答案是不要让你的内容比屏幕宽。 Maybe this is a good answer, but in general it's not very helpful and in my particular situation I don't know how to make my content fit. 也许这是一个很好的答案,但总的来说它不是很有帮助,在我的特殊情况下,我不知道如何使我的内容适合。

Are there better methods for preventing horizontal scrolling? 是否有更好的方法来防止水平滚动?

To give you an idea of the problem that's motivating my question, take a look at http://www.tedsanders.com/BetTheBill/ . 为了让您了解激发我的问题的问题,请查看http://www.tedsanders.com/BetTheBill/ So that you can see the problem better, I have highlighted the offending svg element in gray. 为了让你能更好地看到问题,我用灰色突出显示了有问题的svg元素。 When you click the green 'Bet The Bill' button, the svg rotates. 当您单击绿色的“Bet The Bill”按钮时,svg会旋转。 If your window is small, the corners of the gray rectangle sometimes end up pointing off the screen and make horizontal scrolling possible. 如果您的窗口很小,灰色矩形的角有时最终指向屏幕并使水平滚动成为可能。

I've tested this issue on the current versions of Chrome, Android Chrome, Firefox, and IE11. 我已经在当前版本的Chrome,Android Chrome,Firefox和IE11上测试了这个问题。 Only IE11 gives the behavior I want, with no horizontal scrolling. 只有IE11提供了我想要的行为,没有水平滚动。

Edit: Thanks to your helpful answers, I now have a solution. 编辑:感谢您的有用答案,我现在有了解决方案。 I'm going to implement it soon, but unfortunately that means my link above, originally meant to illustrate the problem, will no longer illustrate the problem. 我很快就会实现它,但不幸的是,这意味着我上面的链接,原本是为了说明问题,将不再说明问题。 Sorry to all future visitors! 对所有未来的访客抱歉! (Perhaps in hindsight I should have made a fiddle. Although who knows how long that will even last...) (也许事后我应该做一个小提琴。虽然谁知道这会持续多久......)

Edit2: Beware, the javascript solution below does not necessarily work on mobile browsers (in my version of Android Chrome there is significant jitter). 编辑2:注意,下面的JavaScript解决方案不一定适用于移动浏览器(在我的Android Chrome版本中存在明显的抖动)。

Edit3: Aha! 编辑3:啊哈! My friend told me that overflow: hidden; 我的朋友告诉我overflow: hidden; will indeed work, but it needs to applied to the parent div and not the body or html or another ancestor. 确实会工作,但它需要应用于父div而不是身体或html或其他祖先。 This looks like the best solution! 这看起来是最好的解决方案!

Try this: 试试这个:

html {
    overflow-x: hidden;
    width: 100%;
}

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

I believe overflow-x: hidden; 我相信overflow-x: hidden; will only stop the particular element that it is applied to from scrolling, so outer-more elements can still cause the window to scroll. 只会停止滚动所应用的特定元素,因此外部更多元素仍然可以使窗口滚动。 Applying it to html and body should prevent anything which exceeds the width and height of window from causing the window to scroll. 将它应用于htmlbody应该防止超出窗口宽度和高度的任何东西导致窗口滚动。

Adding width: 100%; 添加width: 100%; will force the html and body tags to be exactly 100% the width of the window. 将强制htmlbody标签正好是窗口宽度的100%。


But in your example that's not the problem. 但在你的例子中,这不是问题。 For some reason the <div class="container"> sometimes displays another set of scrollbars just for the container and the scrollbars appearing and disappearing is what causes the container's movement. 由于某种原因, <div class="container">有时会为容器显示另一组滚动条,并且出现和消失的滚动条是导致容器移动的原因。

溢出

You can fix it by adding to following: 您可以通过添加以下内容来修复它:

/* overflow: hidden; stops the second set of scrollbars */
/* I increased the width by 300px and added 150px padding on either side. This stopped the grey background from disappearing when the pie chart rotated. */
.container {
    overflow: hidden;
    width: 930px;
    padding-left: 150px;
    padding-right: 150px;
}
    var offset = window.pageXOffset;
    $(window).scroll(function () {
        if(offset != window.pageXOffset)
            window.scrollTo(0, window.pageYOffset);
    });

Also do not forget to hide overflow. 另外别忘了隐藏溢出。

Aha! 啊哈! My friend gave me an answer so I came back here to post it for all of you. 我的朋友给了我一个答案,所以我回到这里为大家发帖。 overflow: hidden; will indeed work, if it is applied to the parent div and not the body or html or another ancestor. 如果它适用于父div而不是bodyhtml或其他祖先, 确实会起作用。 And unlike the javascript solution kindly provided by user3796431, it even works on mobile. 与用户3796431友情提供的javascript解决方案不同,它甚至适用于移动设备。

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

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