css - 修复网页上的表头

标签 css web stylesheet fixed-header-tables

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

我还想知道如何修复网页上的某个元素,使其即使在我们向下滚动时也始终显示。图片可以是文字。使用 div 和 css

最佳答案

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

例子:

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>

关于css - 修复网页上的表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136586/

相关文章:

java - 是否可以使用 -fx-graphic 从 JavaFX CSS 样式表上的 jar 中引用图标?

html - 我怎样才能拥有一个定位其整个父标题标签的 anchor 标签?

javascript - jQuery - 创建下拉工具栏组件

html - Facebook Pixel 激活 2 次

javascript - Jquery each 和 Selector 的行为不同

ios - iOS:适用于iOS应用的样式表/主题

javascript - 无法将 jquery 元素附加到新包装的元素

javascript - 在 jQuery 中同时定位 ID 和类

python - 如何在 Beautiful Soup 4 (Python) 中使用搜索栏

html - 我怎样才能找出是什么 css 覆盖了我的样式表中的 css?