js实现table 表头顶部固定方法

这篇博客介绍了如何使用HTML和CSS实现一个顶部固定表头的布局,特别是在数据表格内容过多需要滚动时,保持表头始终可见。示例代码中,通过设置`margin-bottom`负值和`overflow:auto`属性,实现了表头与内容区域的分离并保证了表头的固定显示。JavaScript部分用于同步表头和内容列的宽度,确保布局的一致性。

业务需求需要顶部固定表头方式实现。

<div>
    <div class="table-head" style="margin-bottom: -20px;">
        <table class="table table-striped">
            <thead>
            <th>期数</th>
            <th>时间</th>
            <th>本月还款</th>
            <th>本月本金</th>
            <th>本月利息</th>
            <th>已还本金总额</th>
            <th>已还利息总额</th>
            <th>累计还款</th>
            </thead>
        </table>
    </div>
    <div class="table-body" style="overflow:auto;height: 300px;">
        <table class="table table-striped">
            <tbody id="result">
            </tbody>
        </table>
    </div>
</div>
<script>
$('.table-head table th').each(function (k, v) {
    let width = $(v).width();
    $('.table-body table tr').each(function (key, val) {
        $($(val).find('td')[k]).width(width);
    });
});
</script>

 爱资料工具-好用的在线工具箱icon-default.png?t=M0H8https://www.toolnb.com/2022年最新房贷计算器 - 爱资料工具icon-default.png?t=M0H8https://www.toolnb.com/tools/fddb.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值