js 固定表头及固定列的js

//给table 外层加一个div然后将divid传入方法,arr是一个数组里面填写需要固定的列数
function fixedCol(arr,divId){
var colArr = arr || [];
colArr.forEach(function(val){
$("#"+divId).scroll(function(){
var left = $("#"+divId).scrollLeft(); // 获取盒子滚动距离
var trs = $("#"+divId+" table tr"); // 获取表格所有的tr
// 获取每一行下面的td或者th,设置相对定位,偏移距离为盒子滚动的距离即 left
trs.each(function(i){
$(this).children().eq(val).css({"position":"relative","top":"0px","left":left,"background":"white"});
});
});
});
}

#spmx{
position: relative;
background: #fff;
z-index: 4;
}
#reyDiv{
z-index: 3;
}

$(document).ready(function(){
fixedCol([0,1,2],"spmx");

});
 
posted @ 2019-01-10 10:54  小任猿  阅读(4391)  评论(0编辑  收藏  举报