画面に収まらないヘッダーがたくさんある HTML テーブルがあります。行数が多いため、垂直方向に完全に機能するスティッキー ヘッダーを使用します。
残念ながら、水平スクロールでも粘着性が維持されます。水平スクロールを許可し、垂直スクロール用に固定ヘッダーを保持するようにコードを変更するにはどうすればよいですか?
テーブル自体は単純明快です。
<table id="calctable">
<thead class="fixed">
<tr id="table-head">
<th><!--Loads of them--></th><th><!--Continues like forever--></th>
</tr>
</thead>
<tbody>
<tr><td><!--And even more of this kind...--></td></tr>
<tr><td><!--And even more of this kind...--></td></tr>
</tbody>
</table>
<table id="header-fixed"></table>
そして私のJavascript(動作しますが...垂直スクロールでのみ):
$(function() {
var tableOffset = $("#calctable").offset().top;
var $header = $("#calctable > thead").clone();
var $fixedHeader = $("#header-fixed").append($header);
$(window).bind("scroll", function() {
var offset = $(this).scrollTop();
if (offset >= tableOffset && $fixedHeader.is(":hidden")) {
$fixedHeader.show();
}
else if (offset < tableOffset) {
$fixedHeader.hide();
}
});
});
そして私のCSS:
#header-fixed {
position: fixed;
top: 0px; display:none;
background-color:white;
}
ありがとうございました!