資料一多,表格往下捲的時候表頭就跟著消失,讀者往往得回捲才能確認欄位意義。固定表頭能明顯改善可讀性,而實作方式其實不複雜。
1. 準備兩個表格,一個正常滾動,一個用 absolute 固定在上方。
<div class="table1-wapper">
<table width="100%" cellpadding="0" cellspacing="0" id="table1">
<tr>
<th><div>序號</div></th>
<th><div>股票名稱</div></th>
...
</tr>
<tr>...</tr>
</table>
</div>
<div class="fixed-table1-wapper">
<table width="100%" cellpadding="0" cellspacing="0" id="fixed-table1"></table>
</div>2. 把表一的 th 複製一份,插入表二
var th_new = $("#table1 tr").eq(0).clone();
$("#fixed-table1").append(th_new);3. 用 resize 即時同步欄寬,讓兩個表格的欄位對齊。
function trResize() {
$("#fixed-table1 th").each(function () {
var num = $(this).index();
var th_width = $("#table1 th").eq(num).width();
$(this).css("width", th_width + "px");
});
}4. 視窗過小時,讓表頭跟著表格一起水平滾動
$(".table1-wapper").scroll(function () {
var scroll = -$(this).scrollLeft();
$(".fixed-table1-wapper").css("left", scroll + "px");
});寬度自適應、去除單元格間隙:<table width="100%" cellpadding="0" cellspacing="0">
表格線:直接對 td 加 border 會出現邊線重疊,記得加上 border-collapse: collapse;

欄寬控制:只要設定第一行的寬度即可,<td width="70"> 或 <td width="20%"> 都可以。
奇偶行不同底色:CSS 寫 #table1 tr:nth-child(2n){background-color:#ccc;} 在 IE7 有相容問題,改用 jQuery 更保險:$("#table1 tr:even").css("background-color","#ccc");
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表格整理</title>
<link rel="stylesheet" type="text/css" href="css/basic.css">
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<style type="text/css">
html{overflow:auto;}
.table-wapper{width:100%;margin:30px auto;font-size:16px;position:relative;overflow:hidden;}
.table1-wapper{height:200px;overflow-y:auto;}
.table-wapper table{border:1px solid #f00;line-height:2;border-collapse:collapse;white-space:nowrap;}
.table-wapper th{font-size:18px;font-weight:normal;background-color:#dbdbdb;}
.table-wapper td{text-align:center;border:1px solid #f00;}
.fixed-table1-wapper{position:absolute;top:0px;left:0px;background-color:#dbdbdb;}
</style>
</head>
<body>
<div class="table-wapper">
<div class="table1-wapper">
<table width="100%" cellpadding="0" cellspacing="0" id="table1">
<tr>
<th>序號</th><th>股票名稱</th><th>股票代碼</th>
<th>成交</th><th>漲跌幅</th><th>換手率</th><th>行業板塊</th>
</tr>
<tr>
<td>1</td><td>光明乳業</td><td>600597</td>
<td>15.2</td><td>+6.23%</td><td>0.12%</td><td>食品飲料</td>
</tr>
<!-- 其餘資料列依此類推 -->
</table>
</div>
<div class="fixed-table1-wapper">
<table width="100%" cellpadding="0" cellspacing="0" id="fixed-table1"></table>
</div>
</div>
<script type="text/javascript">
$(function () {
$("#table1 tr:even").css("background-color", "#ccc"); // 奇偶行底色
var inner_width = $("#table1").outerWidth();
$(".fixed-table1-wapper").css("width", inner_width + "px");
var th_new = $("#table1 tr").eq(0).clone();
$("#fixed-table1").append(th_new);
});
$(window).resize(function () {
trResize();
});
$(".table1-wapper").scroll(function () {
var scroll = -$(this).scrollLeft();
$(".fixed-table1-wapper").css("left", scroll + "px");
});
function trResize() {
var inner_width = $("#table1").outerWidth();
$(".fixed-table1-wapper").css("width", inner_width + "px");
$("#fixed-table1 th").each(function () {
var num = $(this).index();
var th_width = $("#table1 th").eq(num).width();
$(this).css("width", th_width + "px");
});
}
</script>
</body>
</html>這個做法的精神是「複製一份表頭浮在上面」,實作門檻低、相容性也不錯。若日後要支援更複雜的场景(例如固定欄、階層表頭),再考慮 CSS position: sticky 或專門的表格元件會更省事。