粘性表格标题
演示:http: //jsfiddle.net/gvee/kJ9xp/
HTML
<table>
<thead>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</thead>
<tr class="sticky-header">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
... etc ...
</table>
CSS
* {
margin: 0;
border: 0;
padding:0;
}
table {
border-collapse: collapse;
margin: 10px;
}
th, td {
width: 50px;
height: 50px;
background-color: #eee;
text-align: center;
border: 1px solid #ddd;
}
.sticky-header {
display: none;
position: fixed;
top: 0px;
}
.sticky-header th {
background-color: red;
}
jQuery
var thead = $('thead');
var th = $('.sticky-header');
var t = $('table');
$(document).scroll(function() {
if ($(this).scrollTop() >= thead.offset().top && $(this).scrollTop() < t.offset().top + t.height() - th.height())
{
th.show();
} else {
th.hide();
}
});