大數據表格需鎖定表頭(凍結行)、鎖定前幾列(凍結列)、或同時鎖定左上角區域(行列皆凍結),滾動時固定區域保持可見,數據區同步滾動。
+--------------------------+ <-- 容器 divTableLayout (overflow:hidden, 固定高寬)
| +----------------------+ |
| | divTableFix (左上角) | | <-- 凍結行+列交叉區,z-index 最高
| +----------------------+ |
| | divTableHead (凍結行) | | <-- 寬度=數據區寬度,左邊留出凍結列寬度
| +----------------------+ |
| | divTableColumn | | <-- 凍結列,高度=數據區高度,頂部留出凍結行高度
| | (凍結列) | |
| +----------------------+ |
| | divTableData (數據區) | | <-- 真實表格,overflow:auto 產生滾動條
| | (overflow:auto) | |
| +----------------------+ |
+--------------------------+
(function($) {
$.fn.freezeTable = function(options) {
var opts = $.extend({
freezeRowNum: 0, // 凍結行數
freezeColumnNum: 0, // 凍結列數
width: '100%', // 容器寬度
height: '400px' // 容器高度
}, options);
return this.each(function() {
var $table = $(this);
var tableId = $table.attr('id') || 'table_' + Math.random().toString(36).substr(2);
$table.attr('id', tableId);
// 1. 創建佈局容器
var $layout = $('')
.css({ width: opts.width, height: opts.height });
$table.after($layout).detach(); // 移出原位,稍後放入數據區
// 2. 創建四個區域 DIV
var html = '';
if (opts.freezeRowNum > 0 && opts.freezeColumnNum > 0)
html += '';
if (opts.freezeRowNum > 0)
html += '';
if (opts.freezeColumnNum > 0)
html += '';
html += '';
$layout.append(html);
var $fix = $('#' + tableId + '_fix');
var $head = $('#' + tableId + '_head');
var $col = $('#' + tableId + '_col');
var $data = $('#' + tableId + '_data');
// 3. 克隆表格到各區域
$data.append($table); // 原表格放入數據區
if ($fix.length) {
var $fixClone = $table.clone(true).attr('id', tableId + '_fixClone');
$fix.append($fixClone);
}
if ($head.length) {
var $headClone = $table.clone(true).attr('id', tableId + '_headClone');
$head.append($headClone);
}
if ($col.length) {
var $colClone = $table.clone(true).attr('id', tableId + '_colClone');
$col.append($colClone);
}
// 4. 隱藏不需要的行/列(通過 CSS 選擇器)
function hideRows($tbl, startRow) {
$tbl.find('tr:gt(' + (startRow - 1) + ')').hide();
}
function hideCols($tbl, startCol) {
$tbl.find('tr').each(function() {
$(this).find('td:gt(' + (startCol - 1) + '), th:gt(' + (startCol - 1) + ')').hide();
});
}
if ($fix.length) { hideRows($fixClone, opts.freezeRowNum); hideCols($fixClone, opts.freezeColumnNum); }
if ($head.length) { hideRows($headClone, opts.freezeRowNum); }
if ($col.length) { hideCols($colClone, opts.freezeColumnNum); }
// 5. 同步滾動
$data.on('scroll', function() {
var scrollLeft = $data.scrollLeft();
var scrollTop = $data.scrollTop();
if ($head.length) $head.scrollLeft(scrollLeft);
if ($col.length) $col.scrollTop(scrollTop);
if ($fix.length) { $fix.scrollLeft(scrollLeft).scrollTop(scrollTop); }
});
// 6. 窗口 resize 重新計算尺寸
$(window).on('resize', function() {
// 重新計算凍結區寬高,同步數據區尺寸
syncDimensions();
});
function syncDimensions() {
// 計算凍結行高度、凍結列寬度
var headH = 0, colW = 0;
if ($head.length) {
headH = $headClone.find('tr:lt(' + opts.freezeRowNum + ')').outerHeight();
$head.height(headH).css('left', colW);
}
if ($col.length) {
colW = $colClone.find('tr:first th:lt(' + opts.freezeColumnNum + '), tr:first td:lt(' + opts.freezeColumnNum + ')').outerWidth();
$col.width(colW).css('top', headH);
}
if ($fix.length) { $fix.width(colW).height(headH); }
// 數據區尺寸 = 容器 - 凍結區
$data.css({ top: headH, left: colW, width: $layout.width() - colW, height: $layout.height() - headH });
}
// 初始化尺寸
syncDimensions();
// 暴露銷毀方法
$table.data('freezeTable', { destroy: function() { $layout.remove(); $table.insertAfter($layout.prev()); } });
});
};
})(jQuery);
// 使用
$('#myTable').freezeTable({ freezeRowNum: 1, freezeColumnNum: 2, height: '500px' });
table { border-collapse: collapse; } 避免雙重邊框導致寬高計算偏差。table-layout: fixed 並指定 th/td width,或在 syncDimensions 中強制同步列寬。clone(true) 保留事件處理器,但注意避免重複綁定(如排序、編輯事件)。overflow:auto 產生滾動條,凍結區 overflow:hidden 隱藏滾動條但同步 scrollLeft/Top。th { position: sticky; top: 0; z-index: 1; background: #fff; }
th:first-child { left: 0; z-index: 2; } /* 左上角最高 */
td:first-child { position: sticky; left: 0; background: #fff; z-index: 1; }
現代瀏覽器(Chrome 56+, Firefox 59+, Safari 11+)支持 position:sticky 實現純 CSS 凍結行列,**零 JS、零 clone、性能極佳**。僅需在表格容器設置 overflow:auto 並固定高度。舊瀏覽器降級用 JS 方案。
jQuery 凍結表格通過「多層疊加 + clone 同步 + 滾動同步」實現,邏輯完整但 DOM 節點翻倍、resize 計算複雜。現代項目**首選 CSS sticky**,僅在必須兼容 IE11 或需複雜交互(如凍結列可編輯、拖拽調整列寬)時使用 JS 方案。