對於動態列寬使用JavaScript。下面是使用jQuery的解決方案:
example jsfiddle(fullscreen)
只有標題行創建一個重複表元素
<table id="panelContainerFixed">
<tr class="header">
<th>ID</th><th>Name</th>
</tr>
</table>
<table id="panelContainer">
<tr class="header">
<th>ID</th><th>Name</th>
</tr>
<tr>
<td>1</td><td>Name-1</td>
</tr>
<tr>
<td>2</td><td>Name-2</td>
</tr>
<tr>
<td>3</td><td>Name-3</td>
</tr>
...
</table>
計算列的寬度,並將這些寬度在重複頭
// cache re-usable variables
var $window = $(window),
$panelContainerFixed = $('#panelContainerFixed'),
$panelContainer = $('#panelContainer'),
$header = $('.header'),
headerPos = $header.offset().top + $header.height();
// set column widths
$panelContainer.find('th').each(function() {
var $that = $(this);
$panelContainerFixed.find('th').eq($that.index()).width($that.width());
});
根據scrollTop顯示/隱藏重複標題
// on window scroll toggle the fixed headers
$window.scroll(function() {
if ($window.scrollTop() > headerPos) {
$panelContainerFixed.show();
} else {
$panelContainerFixed.hide();
}
});