table锁定表头

本文介绍4种固定表头方式,效果如图:

方式一、使用两个table,.tHead作为表头,tBody包含表体;

<style type="text/css">
*{margin:0; padding:0;}
table{width:100%; table-layout:fixed; border-collapse: collapse;}
tr{height:30px;}
th{text-align:left; background:#F0F1F2; border-bottom:1px solid #CCCDD1;} 
td{color:#666666; border-bottom:1px solid #CCCDD1;}
.tHead{position:fixed; left:0; top:0;}
.tBody{margin-top:30px;}
</style>
<table class="tHead">
    <tr>
        <th>title1</th>
        <th>title2</th>
        <th>title3</th>
        <th>title4</th>
        <th>title5</th>
    </tr>
</table>
        
<div class="tBody">
    <table>
        ......
    </table>
</div>

方式二:一个table

<table> 
      <thead> 
        <tr>
            <th>title1</th>
            <th>title2</th>
            <th>title3</th>
            <th>title4</th>
            <th>title5</th>
        </tr>
    </thead>
    <tbody>
        ......
    </tbody>
</table>
<style type="text/css">
*{margin:0; padding:0;}
table{border-collapse: collapse; width:100%; table-layout:fixed; margin:0 auto;}
tr{height:30px;}
th,td{border-bottom:1px solid #CCCDD1;}
thead{position:fixed; top:0px; display:table; width:100%; table-layout:fixed;}
thead tr th{background-color: #F0F1F2; border-top:1px solid #CCCDD1; text-align:left;}
tbody tr{display:table; width:100%; table-layout:fixed;}
tbody tr:first-child{margin-top:30px;}
</style>

方式三、一个table,只滚动tbody,滚动时copy一个表头显示在顶部

<style type="text/css">
*{margin:0; padding:0;}
table {border-collapse: collapse; width:100%; table-layout:fixed; margin:0 auto;}
table thead, table tbody tr {display:table; width:100%; table-layout:fixed;}
table thead{width:calc(100% - 1.5em);}
table tbody {display:block; height:300px; overflow-y:auto;}
tr{height:30px;}
th,td{border-bottom:1px solid #CCCDD1; text-align:center;}
</style>

方式四、一个table,使用jquery插件jquery.freezeheader.js

<style type="text/css">
*{margin:0; padding:0;}
table{border-collapse: collapse; width:100%; table-layout:fixed; margin:0 auto;}
tr{height:30px;}
th{background:#F0F1F2;}
th,td{border-bottom:1px solid #CCCDD1; text-align:center;}
</style>

调用:

在顶部浏览器中创建一个固定的头表:$("table").freezeHeader();

用固定高度创建一个固定的头表:$("table").freezeHeader({ 'height': '300px' });

滚动时,固定表头相对顶部位置:$("table").freezeHeader({ 'offset': '50px' });

转载于:https://www.cnblogs.com/BlackElf/p/5718955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想在 Bootstrap 中实现冻结表头的效果,可以使用 Bootstrap 插件 DataTables。DataTables 是一个功能强大的 jQuery 插件,可以将 HTML 表格转换成具有排序、搜索和分页功能的交互式数据表格,并且可以通过配置实现表头冻结的效果。以下是一些步骤: 1. 在 HTML 中引入 DataTables 和 Bootstrap 的 CSS 文件。 ``` <link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap5.min.css"> <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.1.9/css/fixedHeader.bootstrap5.min.css"> <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.9/css/responsive.bootstrap5.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css"> ``` 2. 在 HTML 中定义一个表格,并添加 DataTables 的标记和类。 ``` <table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%"> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> <th>Column 5</th> </tr> </thead> <tbody> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> <td>Row 1, Column 3</td> <td>Row 1, Column 4</td> <td>Row 1, Column 5</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> <td>Row 2, Column 3</td> <td>Row 2, Column 4</td> <td>Row 2, Column 5</td> </tr> ... </tbody> </table> ``` 3. 在 JavaScript 中初始化 DataTables,并启用固定表头的插件。 ``` <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap5.min.js"></script> <script src="https://cdn.datatables.net/fixedheader/3.1.9/js/dataTables.fixedHeader.min.js"></script> <script src="https://cdn.datatables.net/responsive/2.2.9/js/dataTables.responsive.min.js"></script> <script src="https://cdn.datatables.net/responsive/2.2.9/js/responsive.bootstrap5.min.js"></script> <script> $(document).ready(function() { $('#example').DataTable({ responsive: true, fixedHeader: true }); }); </script> ``` 以上代码中,`responsive: true` 的作用是使表格具有响应式布局,`fixedHeader: true` 的作用是启用固定表头的插件。您可以根据需要调整其他选项和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值