我目前正在尝试制作屏幕右侧的浮动表,同时在左侧显示其他内容。
使用bootstrap class="col-md-6
分隔左右列。
使表格浮动很容易,但我希望它停止浮动在左手div的底部。
我遵循了本教程https://stackoverflow.com/a/8653109/9364403
当我的浮动div到达div的底部时,它会变为position:absolute;
。
然而,当位置设置为绝对时,表格会返回到div顶部的位置,而不会保持在底部。当我向后滚动一点时,桌子会回到漂浮状态。
如何确保flaoting div不会像上面显示评论链接的jsfiddle一样跳回来? (http://jsfiddle.net/Kkv7X/)
当前的html:
<div class="container">
<div class="row" id="">
<div class="col-md-6">
Content for the left hand column
</div>
<div class="col-md-6" style="position:relative;">
<div class="positionfixed" id="fixedcard">
<table class="table" id="recipetable">
<tr class="tablerow">
<th class="tableheader" style="width:45%;">header 1</th>
<th class="tableheader" style="width:25%;">header 2</th>
<th class="tableheader" style="width:30%;">header 3</th>
<th class="tableheader" style="width:30%;">header 4</th>
</tr>
<tr class="tablerow">
<td>row 1</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="tablerow">
<td>row 2</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="tablerow">
<td>row 3</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="tablerow">
<td>row 4</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="tablerow" id="TableTotalRow">
<td>row 5</td>
<td ></td>
<td ></td>
<td ></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div id="stop">
</div>
当前的js:
function checkOffset() {
if($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10){
$('#fixedcard').css('position', 'absolute');
}
if($(document).scrollTop() + window.innerHeight < $('#stop').offset().top){
$('#fixedcard').css('position', 'fixed'); // restore when you scroll up
}
}
$(document).scroll(function() {
checkOffset();
});
目前的css:
.positionfixed{
position: fixed;
}
我做了一个关于我的代码目前正在做什么的jsFiddle:https://jsfiddle.net/fjfkbrtn/12/
感谢。
答案 0 :(得分:0)
当您更改为absolute
时,只需将bottom
CSS属性设置为0px,并在位置返回fixed
时将其更改回来,如下所示:
function checkOffset() {
if ($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10) {
$('#fixedcard').css('position', 'absolute');
$('#fixedcard').css('bottom', '0px'); // ADD THIS
}
if ($(document).scrollTop() + window.innerHeight < $('#stop').offset().top) {
$('#fixedcard').css('position', 'fixed');
$('#fixedcard').css('bottom', 'initial'); // AND THIS
}
}
$(document).scroll(function() {
checkOffset();
});