console
tableFixed($('.z-table .z-table-con table'), 3)
function tableFixed(table, num) {
var _$wrap = table.parents('.z-table');
_$wrap.find('.fixedHead, .fixedList, .fixedLH').remove();
table.find('thead th').each(function(index, el){
$(el).width($(el).width());
$(el).height($(el).height());
});
var _$fixedTh = table.find('thead').clone();
_$wrap.append(_$fixedTh)
_$fixedTh.wrap('<div class="fixedHead"><table></table></div>');
_$wrap.append($('<div class="fixedList"><table><thead></thead><tbody></tbody></table></div>'));
listHead(table, 'thead', num);
listHead(table, 'tbody', num);
var _$fixedLH = _$wrap.find('.fixedList table thead').clone();
_$wrap.append(_$fixedLH)
_$fixedLH.wrap('<div class="fixedLH"><table></table></div>');
function listHead(tableObj, tCon, n) {
var tr_arr = [];
tableObj.find(tCon + " tr").each(function(index, el){
tr_arr[index] = [];
var _$tr = $('<tr></tr>');
for (var i = 0; i < n; i++) {
tr_arr[index][i] = {
col: 0,
row: 0,
el: 1
}
var cn = 0;
if (i > 0) {
cn = tr_arr[index][i-1].col;
if (cn > 1) {
cn--;
tr_arr[index][i] = {
col: cn,
row: tr_arr[index][i].row,
el: 0
}
continue;
}
}
var rn = 0;
if (index > 0) {
rn = tr_arr[index-1][i].row;
if (rn > 1) {
rn--;
tr_arr[index][i] = {
col: tr_arr[index-1][i].col,
row: rn,
el: 0
}
continue;
}
}
var _$el_null = 0
if (tr_arr[index].length > 1) {
for (var k = 0; k < tr_arr[index].length; k++) {
if (tr_arr[index][k].el == 0) {
_$el_null++
}
}
}
var _$el = $(el).find('th, td').eq(i-_$el_null);
tr_arr[index][i] = {
col: parseInt(_$el.attr('colspan')) || 1,
row: parseInt(_$el.attr('rowspan')) || 1,
el: _$el
}
_$tr.append(_$el.clone().css('color','#ff00ff'));
}
tableObj.parents('.z-table').find('.fixedList table:eq(0)').find(tCon).append(_$tr);
});
}
$('.z-table').on('scroll', function(){
var scrollTop = $(this).scrollTop();
var scrollLeft = $(this).scrollLeft();
$(this).find('.fixedHead, .fixedLH').css('top', scrollTop + 'px');
$(this).find('.fixedList, .fixedLH').css('left', scrollLeft + 'px');
})
}
<div class="title">表头固定及列固定</div>
<div class="z-table">
<div class="z-table-con">
<table>
<thead>
<tr>
<th rowspan='2'>thead</th>
<th colspan='2'>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
</tr>
<tr>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
<th>thead</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan='2'>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td colspan="2">1</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td colspan="2">3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
</div>
.z-table{
width:700px;
height: 500px;
overflow: auto;
position: relative;
margin: 20px auto;
}
th, td{
border: 1px solid #ccc;
text-align: center;
}
td{
height: 50px;
background: #ffffff;
}
th{
background: #ccffff;
padding: 8px;
}
.z-table .z-table-con{
position: relative;
}
.z-table .fixedHead{
position: absolute;
top: 0px;
left: 0px;
}
.z-table .fixedList{
position: absolute;
top: 0px;
left: 0px;
}
.z-table .fixedLH{
position:absolute;
top: 0px;
left: 0px;
z-index: 3;
}
.title{
text-align: center;
}