共通的js代码 tabView.js
* 滚动表格 首行首列跟随(需要再表格外层套class为.table-wrap的div)
* 需要给横向滚动列的表头列一个id-用来获取当前列滚动位置
*/
var tabView = {
columnName:"",//获取偏移的列id名
witchColumn:0,//表格中固定的列数(第几列下标,从0开始,隐藏列也算在内)
suff:15,//偏移量(误差值)
witchColumns:[],//需要固定的多列,传入下表数组(从0开始,隐藏列也算在内)
init : function(columnName,witchColumn,suff,witchColumns){
tabView.columnName = columnName;
tabView.witchColumn = witchColumn;
tabView.witchColumns = witchColumns;
if(suff != null && suff !=""){
tabView.suff = suff;
}
},
tableMove : function(){
var l = $("#"+tabView.columnName).position().left;
$(".table-wrap").scroll(function() {
var left = $(".table-wrap").scrollLeft();
var top = this.scrollTop;
var trArr=$(".table-wrap table tr");
var thArr=$(".table-wrap table thead tr th");
left = left - l + tabView.suff;
if(left<=0){
left = 0;
}
trArr.each(function(){
$(this).children().eq(tabView.witchColumn).css({"position":"relative","top":"0px","left":left,"z-index":3});
});
thArr.each(function(index){
$(this).css({"position":"relative","top":top,"left":"0px","z-index":9});
if(index == tabView.witchColumn){
$(this).css({"position":"relative","top":top,"left":left,"z-index":99});
}
});
});
},
tableAllMove : function(){
$(".table-wrap").scroll(function() {
var left = $(".table-wrap").scrollLeft();
var top = this.scrollTop;
var trArr=$(".table-wrap table tr");
var thArr=$(".table-wrap table thead tr th");
trArr.each(function(){
for(var i=0;i<tabView.witchColumns.length;i++){
var c = tabView.witchColumns[i];
$(this).children().eq(c).css({"position":"relative","top":"0px","left":left,"z-index":3});
}
});
thArr.each(function(index){
$(this).css({"position":"relative","top":top,"left":"0px","z-index":9});
for(var i=0;i<tabView.witchColumns.length;i++){
if(index == tabView.witchColumns[i]){
$(this).css({"position":"relative","top":top,"left":left,"z-index":99});
}
}
});
});
}
}
html页面代码示例
<div class="table-wrap">
<table id="contentTable" class="table table-striped table-bordered table-condensed table-hover">
<thead>
<tr>
<th class="hide"></th>
<th>序号</th>
<th id="topTh">列1</th>
<!-- <th>列2</th> -->
<!-- <th>列3</th> -->
<th>列4</th>
<th>列5</th>
<th>列6</th>
<th>列7</th>
<th>列8</th>
<th>列9</th>
<th>列10</th>
<th>列11</th>
<th>列12</th>
<th>列13</th>
<th>列14</th>
<th>列15</th>
<th>列16</th>
</tr>
</thead>
<tbody id="aaa">
<tr>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
<td>1111111</td>
</tr>
<tr>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<td>222222</td>
<tr>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td><td>3333333</td>
<td>3333333</td>
<td>3333333</td><td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td>
<td>3333333</td> <td>3333333</td>
<tr>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td><td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
<td>44444444</td>
</tbody>
</table>
页面引入 tabview.js 然后调用
调用方法://表格 表头 首列移动
tabView.init("topTh",2,0,[1,2]);
tabView.tableAllMove();