<thead id="theadData">
</thead>
<thead id="theadData1">
</thead>
thead复制一份,然后加上css样式
#theadData1{
position: absolute;
top: 0;
}
给table的外部元素加上position: relative;
最后加上滚动监听和宽度设置
$("#theadData1").css("width",$("#theadData").outerWidth());
let dibuTh = $("#theadData th");
let biaomianTh = $("#theadData1 th");
$.each(dibuTh,function (k,v) {
$(biaomianTh[k]).css("width",$(v).outerWidth());
})
//滚动#tabContent,header跟随滚动
$('#tabContent').on('scroll', function(e) {
var leftPx = $(e.target).scrollLeft(); //获取表格body,滚动条距离左边的长度
var left = 'translateX(-' + leftPx + 'px)';
$('#theadData1').css('transform', left); //设置表格header的内容反向(-)移动
});
完工
table固定表头
最新推荐文章于 2024-04-10 15:25:41 发布