首页 文章

HTML,CSS - 禁用正文滚动

提问于
浏览
63

我想完全禁用HTML body 上的滚动 . 我尝试了以下选项:

  • overflow: hidden; (不工作,没有禁用滚动,它只是隐藏了滚动条)

  • position: fixed; (这有效,但它完全滚动到顶部,这对于这个特定的应用是不可接受的)

我无法找到这两个选项的替代方案,还有吗?

3 回答

  • 8

    HTML css工作正常,如果body标签也没有什么可以写的

    <body scroll="no" style="overflow: hidden">
    

    在这种情况下,覆盖应该在身体标签上,它更容易控制,但有时会让人头疼 .

  • 131

    这篇文章很有帮助,但只是想分享一个可以帮助他人的轻微替代方案:

    设置 max-height 而不是 height 也可以解决问题 . 在我的情况下,我基于类切换禁用滚动 . 当容器's height is smaller than that of the viewport would stretch the container, which wouldn' t成为您想要的时候设置 .someContainer {height: 100%; overflow: hidden;} . 为此设置 max-height 帐户,但如果内容更改时容器's height is greater than the viewport' s仍然禁用滚动 .

  • 10

    设置 heightoverflow

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

    http://jsfiddle.net/q99hvawt/

相关问题