1

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

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

代码片段如下:

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/

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

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

4

2 回答 2

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/

于 2013-07-22T09:22:30.457 回答
0

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

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

小提琴

于 2013-07-19T13:16:51.913 回答