我希望尽可能轻松地完成任何其他库。
在我很长的表格中,我想在<tbody>
标签中添加一个滚动条,以便头部始终可见但不起作用。能帮忙吗?
小提琴:http://jsfiddle.net/Hpx4L/
<table border="1">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
<tbody style="overflow-y:scroll; height:100px;"> <!-- wont work -->
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>
答案 0 :(得分:61)
您可以将<tbody>
的内容包装在可滚动的<div>
:
HTML
....
<tbody>
<tr>
<td colspan="2">
<div class="scrollit">
<table>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
...
CSS
.scrollit {
overflow:scroll;
height:100px;
}
看看我的jsfiddle,从你的分叉:http://jsfiddle.net/VTNax/2/
答案 1 :(得分:14)
这些解决方案通常会出现标题列与正文列对齐的问题,并且在调整大小时可能无法正常工作。我知道你不想使用额外的库,但如果你碰巧使用jQuery,那么这个很小。它支持固定的页眉,页脚,列跨越(colspan),水平滚动,调整大小以及在滚动开始之前显示的可选行数。
jQuery.scrollTableBody (GitHub)
只要您的表格包含正确的<thead>
,<tbody>
和(可选)<tfoot>
,您只需要这样做:
$('table').scrollTableBody();
答案 2 :(得分:7)
这是因为您要在表格<tbody>
之前添加<td>
代码,但如果没有<td>
则无法打印任何数据。
因此,您必须<div>
说#header
与position: fixed;
header
{
position: fixed;
}
制作另一个<div>
作为<tbody>
tbody
{
overflow:scroll;
}
现在您的标题已修复,正文将滚动。标题将保留在那里。
答案 3 :(得分:-1)
如果您不想将表格包装在任何 div :
下table{
table-layout: fixed;
}
tbody{
display: block;
overflow: auto;
}