我正在尝试在滚动页面时制作粘性表格。目前我有两张桌子一个接一张,但是当我滚动页面时,只有第一张桌子会移动10px;
和停止。这是我的JSFiddle,我的桌子根本不动。任何人都可以建议我,我做错了什么?
HTML:
<div id="header">
header
</div>
<table class="table_filter_data filter_table" cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td>
table_1
</td>
</tr>
</table>
<table class="table_filter_data filter_table" cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td>
table_2
</td>
</tr>
</table>
jQuery:
<script type="text/javascript">
function UpdateFilterHeaders() {
$("div.div_filter_table").each(function() {
var originalTable = $(".tableFloatingHeaderOriginal", this);
var floatingTable = $(".tableFloatingHeader", this);
var offset = $(this).offset();
var scrollTop = $(window).scrollTop();
if ((scrollTop > offset.top) && (scrollTop < offset.top + $(this).height())) {
floatingTable.css("visibility", "visible");
originalTable.css("visibility", "hidden");
floatingTable.css("top", Math.min(scrollTop - offset.top, $(this).height() - floatingTable.height()) + "px");
// Copy cell widths from original header
$(floatingTable).each(function(index) {
var cellWidth = $(originalTable).eq(index).css('width');
$(this).css('width', cellWidth);
});
// Copy row width from whole table
floatingTable.css("width", $(this).css("width"));
}
else {
originalTable.css("visibility", "visible");
floatingTable.css("visibility", "hidden");
floatingTable.css("top", "0px");
}
});
}
$(document).ready(function() {
$("table.filter_table").each(function() {
$(this).wrap("<div class=\"div_filter_table\" style=\"position:relative\"></div>");
var originalTable = $(this);
var clonedTable = originalTable.before(originalTable.clone()
.addClass("tableFloatingHeader")
.css("position", "absolute")
.css("top", "0px")
.css("left", $(this).css("margin-left"))
.css("visibility", "hidden"));
originalTable.addClass("tableFloatingHeaderOriginal");
});
UpdateFilterHeaders();
$(window).scroll(UpdateFilterHeaders);
$(window).resize(UpdateFilterHeaders);
});
</script>
CSS:
body {height:2000px;}
.table_filter_data{ margin: 0px ; padding: 0px; padding-left: 3px; padding-top: 10px;}
.table_filter_data > tbody > tr > td{ padding: 2px; text-align: left; font-size: 0.9em;}
.table_filter_data > tbody > tr > td.title{ text-align: right;}
.table_filter_data > tbody > tr > td > input{ font-size: 0.9em;}
.table_filter_data > tbody > tr > td > select{ font-size: 0.9em;}
#header {height: 100px; border: 1px solid gray}