【问题标题】:Horizontal scrollable sticky HTML-Table-Header水平可滚动的粘性 HTML-Table-Header
【发布时间】:2014-02-28 13:43:12
【问题描述】:

我有一个 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;
}

谢谢!

【问题讨论】:

  • 我前段时间遇到过这个问题:mkoryak.github.io/floatThead 是解决这个问题的好插件。它唯一的问题是它在标题元素容器上强制使用overflow:hidden;。因此,如果您想在标题中包含 CSS 下拉列表等,您需要将它们设置为 position:fixed。但这是一个非常特殊的用例。

标签: javascript jquery html css tableheader


【解决方案1】:

非常感谢 Nico O,我能够在几秒钟内解决这个问题。

我只需要添加另一个 Javascript:

<script src="https://rawgithub.com/mkoryak/floatThead/master/dist/jquery.floatThead.min.js">

完全删除了上面的(现在已经过时的)CSS-Information,只用一行替换了Javascript:

       $(function() {                
            $('#calctable').floatThead();
        });

工作!!!

【讨论】:

  • 如果我是你,我会在本地下载该脚本,将来我可能会将其从该位置移走。
  • 已经搞定了,这个项目是在没有网络的电脑上运行的:)
猜你喜欢
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多