您需要设置边栏的高度。现在,您的侧栏与您的文本块一样高,并在视口下方运行(但您看不到)。如果您将其高度设置为100%(或其他),则边栏将显示滚动条,如果其所有内容不能一次显示。
所以,你需要改变这一点:
div#fixed-div {
position: fixed;
left: 0;
top 80px;
width: 260px;
background-color: silver;
overflow-y: scroll;
}
要这样:
div#fixed-div {
position: fixed;
left: 0;
top 80px;
width: 260px;
background-color: silver;
overflow-y: scroll;
height: 100%;
}
编辑:
如果你想最大的浏览器支持(甚至老的浏览器)的解决方案,你需要JavaScript。下面是依赖于jQuery的一个解决方案:
CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
div#header-div {
height: 90px;
background-color: lime;
}
div#fixed-div {
position: fixed;
width: 260px;
background-color: silver;
overflow-y: scroll;
}
的JavaScript:
var bodyHeight = $('body').height();
var headerHeight = $('#header-div').height();
var sidebarHeight = bodyHeight - headerHeight;
$('#fixed-div').height(sidebarHeight);
工作的jsfiddle:http://jsfiddle.net/nH7xR/
请参阅在http://stackoverflow.com响应/ questions/15650278/how-to-set-fixed-position-element-height-to-reach-bottom-of-browser-window for a solution that does not require me to rest the html and body elements,or use jQuery。 – 2013-03-27 13:52:05
我喜欢'底:0;'技巧,我不知道你能做到这一点。感谢您指出!它在旧版浏览器中不起作用,顺便说一下:http://stackoverflow.com/a/5070238/1064286 – 2013-03-27 16:53:27