javascript - 滚动数据时如何保持标题固定

标签 javascript jquery html css

我有两个表,第一个用作使用日期选择器的搜索功能,第二个包含 theadtbody,后者包含我的数据的实际标题和行.我尝试关注 http://jsfiddle.net/wLcjh/255/

var tableOffset = $("#table-1").offset().top;
var $header = $("#table-1 > thead");
var $fixedHeader = $("#header-fixed").append($header.clone());
console.log(tableOffset);
console.log($header);
console.log($fixedHeader);
console.log($fixedHeader.is(":hidden"));

$(window).bind("scroll", function () {
    var offset = $(this).scrollTop();

    if (offset >= tableOffset && $fixedHeader.is(":hidden")) {
        $fixedHeader.show();

        $.each($header.find('#tr1 > th'), function (ind, val) {
            var original_width = $(val).width();
            $($fixedHeader.find('#tr1 > th')[ind]).width(original_width);
        });
    }

});

但是,它对我的​​页面没有任何影响。甚至有可能完成吗?

最佳答案

如何使用普通 CSS 使用 postion:sticky 来做到这一点? ?

body { height: 1000px; }
thead tr th{
    background: white;
    position: sticky;
    top: 0;
  }
<table id="table-1">
    <thead id="header-fixed">
        <tr>
            <th >Col1</th>
            <th >Col2</th>
            <th >Col3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>info000000</td>
            <td>info</td>
            <td>info</td>
        </tr>
        <tr>
            <td>info</td>
            <td>infolllllllllllll</td>
            <td>info</td>
        </tr>
        <tr>
            <td>info</td>
            <td>info</td>
            <td>info00000000</td>
        </tr>
    </tbody>
</table>

关于javascript - 滚动数据时如何保持标题固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49304938/

相关文章:

javascript - 用 & 字符替换搜索输入框中的所有空格

javascript - 用数组动态填充多维数组

javascript - 无法阻止 javascript 中的默认事件

javascript - javascript 混淆器是否适用于 AngularJS?

javascript - jQuery ~ 在跟随之前调整 href

javascript - 在 Bootstrap 下拉菜单中悬停时显示事件父菜单项

javascript - 如何使用 Javascript 在 CSS 中动态设置背景图片 url

jquery - 为什么页脚没有显示在页面底部?

html - 为什么 Font Awesome 不能在 JSFiddle 中使用 Bootstrap?

javascript - window.onload = function() 的两个 JS 文件冲突