对于动态列宽,请使用 JavaScript。这是使用 jQuery 的解决方案:
示例 jsfiddle(全屏)
创建仅包含标题行的重复表格元素
<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();
}
});