0

即使我们在网站上向下滚动并离开表格视图,我也想知道如何修复表格标题。我想使用 css 样式来做到这一点。谢谢你。

我还想知道如何修复网页上的元素,以便即使我们向下滚动它也总是出现。图片可以是文字。使用 div 和 css

4

2 回答 2

1

您可以通过点击窗口上的滚动事件处理程序来执行类似的操作,并使用另一个具有固定位置的表格在页面顶部显示标题。

例子:

var tableOffset = $("#table-1").offset().top;
var $header = $("#table-1 > 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();
    }
});
body { height: 1000px; }
#header-fixed { 
    position: fixed; 
    top: 0px; display:none;
    background-color:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table-1">
    <thead>
        <tr>
            <th>Header1</th>
            <th>Header2</th>
            <th>Header3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>info</td>
            <td>info</td>
            <td>info</td>
        </tr>
        <tr>
            <td>info</td>
            <td>info</td>
            <td>info</td>
        </tr>
        <tr>
            <td>info</td>
            <td>info</td>
            <td>info</td>
        </tr>
    </tbody>
</table>
<table id="header-fixed"></table>

于 2015-06-30T11:45:03.457 回答
0

取自我的一篇旧文章,这是您希望在一个小提琴中一起完成的两件事的示例。

JSFiddle

查询:

function moveScroll() {

var scroll = $('#table-container').offset().top;
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",
    left: $("#maintable").offset().left + 'px',
    top: 130
}).width($("#maintable").width());

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

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

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

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

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

$('#table-container').scroll(moveScroll);
于 2015-06-30T11:37:48.173 回答