有没有人知道像position: fixed元素一样把页脚粘在div底部的方法。我发现的所有方法都把它放在起始视图的底部,或者放在底部,所以你必须一直向下滚动才能看到它们。
我尝试过将父对象设置为position:relative,将子对象设置为position: absolute,也使用了display: table-cell和vertical-align: bottom,但都没有在滚动时将其固定在原地,而是在div底部或默认视图的底部保持不动。
.a{
position:relative;
background-color:#ccc;
width:500px;
min-height: 150px;
max-height: 150px;
overflow-y: scroll;
float:left;
padding:8px;
}
.footer{
position:absolute;
bottom:0;
left:0;
width:100%;
background-color:#666;
color:#fff;
}<div class="a">
Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />
<div class="footer">Some text</div>
</div>
发布于 2015-11-10 09:11:04
你真的不能,不能以你想要的方式,你需要使用jquery来不断移动位置。
你能做的,也是最简单的解决方案,就是用一个容器包裹整个区域。
.outer {
position:relative;
width:500px;
}
.a{
background-color:#ccc;
min-height: 150px;
max-height: 150px;
overflow-y: scroll;
padding:8px;
}
.footer{
position:absolute;
bottom:0;
left:0;
width:100%;
background-color:#666;
color:#fff;
}
<div class="outer">
<div class="a">
Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />Some content<br />
</div>
<div class="footer">Some text</div>
</div>发布于 2015-11-10 09:07:56
这里的问题是,您不能在元素上使用position: fixed (只适用于viewport),但是您希望footer元素相对于其父元素位于fixed位置。
您可以通过包装可滚动的div并将footer设置为该wrap元素的底部来实现此目的。由于wrap不会滚动(它内部的元素会滚动),所以当您滚动时,footer也不会移动。
请参阅此fiddle。
html, body {
margin: 0;
padding: 0;
}
.wrap {
background-color:#ccc;
position: relative;
}
.a {
height: 150px;
overflow-y: scroll;
}
.b {
margin-top: 300px;
padding-bottom: 20px;
}
.footer{
position:absolute;
bottom:0;
left:0;
background-color:#666;
color:#fff;
width: 100%;
}<div class="wrap">
<div class="a">
top
<div class="b">bottom</div>
</div>
<div class="footer">Some text</div>
</div>
https://stackoverflow.com/questions/33621120
复制相似问题