js 固定表頭及固定列的js

小任猿發表於2019-01-10
//給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");

});
 

相關文章