我有一个表,位于页面上的容器中。当用户滚动过去时,我试图让表格的标题粘在页面顶部。我尝试了多种方法来使标题具有粘性,但我运气不佳。
表数据正在JS中生成。
任何帮助都会很棒!
超文本标记语言
<div class="container-fluid">
<div id="userTable" class="sticky-table">
<table id="ticketsTable">
<thead id="head" class="sticky-header"</thead>
<tbody id="body">
</tbody>
</table>
</div>
</div>
JavaScript
function generateTableHeader() {
var headerArray = generateHeaderArray(),
headerString = "<thead id='head'><tr>" + "<th></th>";
if (!headerArray.length) {
$("#head").empty();
$("#userTable").append("<h1 id='noTicketsFound'>No tickets found.</h1>");
return;
}
headerOrder.forEach(function(key) {
var isChecked = key;
if (!$(".dropdown-menu-fixed #" + key).is(":checked")) {
isChecked += " uncheckedColumn";
}
headerString += "<th data-property='" + key + "' class='sortableHeader " + isChecked + "'>" +
dictionary[key] + "</th>";
});
headerString += "</tr></thead>";
// replaceWith seems faster than separate calls to empty then append.
$("#head").replaceWith(headerString);
// Add SORTCLASS to SORTPROPERTY column, since that is already sorted.
$(".sortableHeader." + SORTPROPERTY).addClass(SORTCLASS);
}
使标题粘在滚动功能上
function stickyTableHeader() {
$(".sticky-table").each(function() {
var el = $(this),
offset = el.offset(),
scrollTop = $(window).scrollTop(),
stickyHeader = $(".stickyHeader", this);
if (scrollTop > offset.top && scrollTop < offset.top + el.height()) {
stickyHeader.css({
visibility: "visible"
});
} else {
stickyHeader.css({
visibility: "hidden"
});
}
});
}
慕姐8265434
猛跑小猪
料青山看我应如是
相关分类