我看到一些关于类似问题的提问,但是没有一个指定的答案实际上适用于这个问题。
考虑以下代码片段:
$(function () {
$(window).on('scroll', function () {
/**
THIS SHOULD NOT BE CALLED!!!
So, change some colors to warn about it, if it happens.
*/
$('#content').css('background-color', 'red');
});
});
body {
margin:0;
padding:0;
height: 100%;
width: 100%;
}
#container {
position: absolute;
height: 100%;
width: 100%;
z-index: 9999999;
overflow: auto;
}
#nav {
background-color:rgb(50,50,50);
color: white;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 30px;
padding-top: 10px;
z-index: 100;
}
#content-wrapper {
background-color:rgb(200,200,200);
min-height: 100%;
height: auto !important;
width: 100%;
z-index:2;
}
#content {
padding-top: 40px;
padding-bottom: 40px;
}
#footer {
background-color: rgb(220, 220, 240);
position: fixed;
left: 0;
bottom: 0;
height: 30px;
width: 100%;
padding-top: 10px;
z-index: 9999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="nav">
Navbar
</div>
<div id="content-wrapper">
<div id="content">
<div>
Begin
</div>
<div style="height: 600px;">
...
</div>
<div>
End
</div>
</div>
</div>
<div id="footer">
Footer
</div>
</div>
nav
和footer
下方。由于这非常重要,只有container
元素滚动(BODY
元素不应滚动),我该如何解决?是否可能?HTML结构应该基本上符合此问题的建议(固定导航栏、全高度内容等)。我尝试了几个技巧:修改
z-index
,包装各种东西等,但我感到很困惑。目标浏览器是Google Chrome,因为这是该应用程序中使用的浏览器。理想的解决方案将使固定元素调整其宽度以弥补对
container
元素的overflow: auto;
。