表格一長就要滾動,但滾下去之後表頭消失、使用者根本搞不清楚每欄是什麼。解法是「浮動表頭」:另外準備一份 thead 副本,滾動越過原始表頭時就把它貼在視窗頂端。
<style type="text/css">
#scroll_head {
position: absolute;
display: none;
}
</style>
<script type="text/javascript">
//該函數在上一個table數據加載完成後調用
//把表頭的寬度設置到浮動的頁頭去
var copyWidth = function () {
var b = $('#data_tbody').prev().find('tr:last').find('th');
var c = $('#scroll_head').find('tr:last').find('th');
for (var i = 0; i < b.length; i++) {
var newWith = b.eq(i).width();
if ($.browser.msie) { newWith += 1; }
c.eq(i).width(newWith);
}
}
$(function () {
$(window).scroll(function () {
if ($('#data_tbody').length > 0) {
var thead = $('#data_tbody').prev();
var thOffset = thead.offset();
var scTop = $(window).scrollTop();
if (scTop > thOffset.top) {
//滾過表頭,改用浮動副本
$('#scroll_head').css('display', 'block');
$('#scroll_head').offset({ top: scTop, left: thOffset.left });
} else {
//還在表頭上方,還原成原始表頭
$('#scroll_head').css('display', 'none');
}
}
});
});
</script>
<div id="data_div">
<table>
<thead>
<tr>
<th class="tt1" colspan="2">一級1</th>
<th class="tt2" colspan="5">一級2</th>
<th class="tt3" colspan="6">一級3</th>
</tr>
<tr>
<th style="width: 23px;">二級11</th>
<th style="width: 36px;">二級12</th>
<th class="tt" style="width: 40px;">二級21</th>
<th class="tt" style="width: 30px;">二級22</th>
<th class="tt" style="width: 30px;">二級23</th>
<th class="tt" style="width: 30px;">二級24</th>
<th class="tt" style="width: 30px;">二級25</th>
<th class="tt" style="width: 30px;">二級31</th>
<th class="tt" style="width: 30px;">二級32</th>
<th class="tt" style="width: 30px;">二級33</th>
<th class="tt" style="width: 30px;">二級34</th>
<th class="tt" style="width: 30px;">二級35</th>
<th class="tt" style="width: 30px;">二級36</th>
</tr>
</thead>
<tbody id="data_tbody">
數據內容,加載完成後調用 copyWidth()
</tbody>
</table>
</div>
<div id="scroll_head" style="display:none;">
<table width="100%">
<thead>
<tr>
<th class="tt1" colspan="2">一級1</th>
<th class="tt2" colspan="5">一級2</th>
<th class="tt3" colspan="6">一級3</th>
</tr>
<tr>
<th style="width: 23px;">二級11</th>
<th style="width: 36px;">二級12</th>
<th class="tt" style="width: 40px;">二級21</th>
<th class="tt" style="width: 30px;">二級22</th>
<th class="tt" style="width: 30px;">二級23</th>
<th class="tt" style="width: 30px;">二級24</th>
<th class="tt" style="width: 30px;">二級25</th>
<th class="tt" style="width: 30px;">二級31</th>
<th class="tt" style="width: 30px;">二級32</th>
<th class="tt" style="width: 30px;">二級33</th>
<th class="tt" style="width: 30px;">二級34</th>
<th class="tt" style="width: 30px;">二級35</th>
<th class="tt" style="width: 30px;">二級36</th>
</tr>
</thead>
</table>
</div>
寬度必須用 JS 同步,不能只靠 CSS。兩份表頭是各自獨立的 table,瀏覽器會依內容重新計算欄寬,因此就算 HTML 裡寫死了 width,仍可能對不齊。copyWidth() 的作用就是在資料載入後把實際渲染出來的寬度量回來套上去。這一步很容易遺漏,症狀就是「表頭和資料就差了那麼幾像素」。
浮動表頭要給不透明背景。否則滾動時會看穿底下的資料,兩層文字疊在一起。範例中的 thead 建議加上深色底色正是這個原因。
$.browser 在 jQuery 1.9 之後已被移除。範例裡 if ($.browser.msie) 這段在新版 jQuery 會直接報錯,實作時請刪掉,或改用特性偵測判斷。
scroll 事件請加 debounce 或 throttle。滾動事件的觸發頻率非常高,每次都做 offset 查詢與 DOM 寫入容易卡頓。用 requestAnimationFrame 節流是成本最低的做法。
更現代的替代方案:直接用 CSS position: sticky。如果是新專案,與其維護兩份表頭,不如在 th 上寫:
thead th {
position: sticky;
top: 0;
background: #fff;
z-index: 1;
}
瀏覽器原生處理,不必寫一行 JS,多層表頭也能各自指定不同的 top 值。上面這套 JS 方案的價值主要在維護既有舊系統,或需要相容相當老舊的瀏覽器時。