【问题标题】:Fixed table headers with a fixed banner div带有固定横幅 div 的固定表头
【发布时间】:2018-10-06 23:48:03
【问题描述】:

我已经使用this SO question 中的代码成功创建了固定表头,但是在将其调整到我的页面时遇到了一些我无法弄清楚的问题。我对 Javascript/jQuery 有点陌生,所以请多多包涵。

代码依赖浏览器中的滚动事件来检测 THEAD 不在视野范围内,因此它知道何时克隆表并将克隆的标题放在页面顶部。
但是,我的页面在页面顶部固定有一个 DIV,其中包含一个搜索栏等,当它存在时,固定标题不起作用。由于我需要固定区域,因此我正在努力寻找解决方法。这可能真的很简单,但我没有看到它。

sn-p代码如下:

function moveScroll() {
    var scroll = $(window).scrollTop();
    var anchor_top = $("#maintable").offset().top;
    var anchor_bottom = $("#bottom_anchor").offset().top;

    if (scroll > anchor_top && scroll < anchor_bottom) {
        clone_table = $("#clone");

        if (clone_table.length === 0) {
            clone_table = $("#maintable").clone();
            clone_table.attr({
                id: "clone"
            }).css({
                position: "fixed",
                "pointer-events": "none",
                top: 0
            }).width($("#maintable").width());

            $("#table-container").append(clone_table);

            $("#clone").width($("#maintable").width());

            $("#clone").css({
                border: "none"
            });

            $("#clone thead").css({
                visibility: "true"
            });

            $("#clone tbody").css({
                visibility: "hidden"
            });

            var footEl = $("#clone tfoot");
            if (footEl.length) {
                footEl.css({
                visibility: "hidden"
            });
        }
    }
    } else {
        $("#clone").remove();
    }
}

$(window).scroll(moveScroll);

这是一个带有精简版页面的 JSFiddle。

http://jsfiddle.net/urvfE/

如果您删除 #topsection#table-container 的 CSS 部分,您将看到固定的标题在起作用。当这些部分出现时,没有任何作用。

顺便说一句,我还有另一个问题。如果我在桌子上使用border-collapse:collapse 来获得漂亮的边框,Firefox 不会正确呈现固定标题。它会在顶部呈现一个完整的空表。任何想法如何规避这个?

【问题讨论】:

    标签: jquery css fixed-header-tables


    【解决方案1】:

    周末后我回到这个问题并解决了它。我不敢相信我上周看不到这个!它证明了一双新鲜的眼睛可以做什么。
    我想我会在这里回答,以防其他人喜欢这个代码。


    我将其中一个变量更改为收听主要部分的顶部而不是整个窗口:

    var scroll = $(window).scrollTop();
    

    现在:

    var scroll = $('#table-container').offset().top;
    


    然后我改语句调用函数:

    $(window).scroll(moveScroll);
    

    现在:

    $('#table-container').scroll(moveScroll);
    


    最后我手动将固定标题的顶部设置为 130px 以匹配顶部的底部。

    这是一个小提琴:http://jsfiddle.net/hvRZy/


    我仍然无法解决边界崩溃问题,所以如果有人在这方面有任何想法,那就太棒了。谢谢!


    编辑:我已经设法通过以下CSS(还添加了圆角)解决了边框问题:

    table {
        border-collapse: separate;
        border-spacing: 0px;
    }
    table tr th, table tr td {
        border-right: 1px solid #000;
        border-bottom: 1px solid #000;
        padding: 5px;
    }
    table tr th:first-child, table tr td:first-child {
        border-left: 1px solid #000;
    }
    table tr th {
        background: #c0c0c0;
        border-top: 1px solid #000;
        text-align: center;
    }
    table tr:first-child th:first-child {
        border-top-left-radius: 6px;
    }
    table tr:first-child th:last-child {
        border-top-right-radius: 6px;
    }
    table tfoot tr:last-child td:first-child {
        border-bottom-left-radius: 6px;
    }
    table tfoot tr:last-child td:last-child {
        border-bottom-right-radius: 6px;
    }
    

    最后的小提琴! http://jsfiddle.net/KfxQg/

    【讨论】:

      【解决方案2】:

      您可以使用jQuery TH Float Plugin。如果你想让table th 被修复,你可以像这样使用它

      $("#maintable").thfloat({
        attachment : "#maintable",
        noprint : false
      });
      

      FIDDLE

      【讨论】:

      • 我尽量避免使用第三方插件,因为我希望自己能够维护它,特别是因为我拥有的代码可以可靠地工作。奇怪的是,该插件似乎也在与边界折叠作斗争,至少在小提琴中是这样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      相关资源
      最近更新 更多