CSS 滚动固定定位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/696827/
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-29 20:28:20  来源:igfitidea点击:

Fixed Positioning With Scrolling

css

提问by Kezzer

If, for example, I have a menu using fixed positioning but it's larger than the height of the current window, is there a way to allow this to scroll? The browser's default behaviour is to just hide it, and not let you access it.

例如,如果我有一个使用固定定位的菜单但它大于当前窗口的高度,有没有办法让它滚动?浏览器的默认行为只是隐藏它,而不让您访问它。

div#sidebar {
    position:fixed;
    top:30px;
    left:0;
    bottom:4px;
    width:148px;
    background-color:#d7d7d7;
}

Here's a snippet of what I've currently got. Would it require some JavaScript or something along those lines?

这是我目前所拥有的一个片段。它是否需要一些 JavaScript 或类似的东西?

EDIT:I'm not sure if this is actually possible to get correct. I want an element which is 30 pixels from the top of the document. I want to allow this to scroll using overflow:auto and height:100%. Either way I seem to do it, the scrollbar will be hidden, or a portion of the div will be hidden.

编辑:我不确定这是否真的可能正确。我想要一个距离文档顶部 30 像素的元素。我想允许它使用溢出:自动和高度:100% 滚动。无论哪种方式我似乎都这样做,滚动条将被隐藏,或者 div 的一部分将被隐藏。

回答by Seb

You'll need to set overflow:scroll(or overflow:auto) to that div, and set height to 100%.

您需要将overflow:scroll(或overflow:auto) 设置为该 div,并将高度设置为 100%。