HTML表头固定,表体滚动方案

在前端开发过程中,经常会遇到一个需求就是,需要一个表格展示数据,

1、表格头纵向位置固定,纵向滚动时,若表体内容超长时,表头不能移动,表体内容滚动显示;

2、表格头横向随表格内容横向滚动;

效果展示:


关键:

1、表头(意义上的)和表体(意义上的)都用一个table来显示;

2、因为表体可以纵向滚动,右边多出一个纵向滚动条,所以表体的宽度要比表头的宽度多出17个像素,也就是说,如果要设置表格的宽度是500的话,表体的宽度为100%,那表头的宽度就是减去17个像素,就是calc(100% - 17px);

3、列宽要固定,两个table节点的table-layout样式为fixed

全部代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .table {
            border: solid 1px #ddd;
            width: 100%;
            display: inline-block;
        }

        th, td {
            border-right: solid #ddd 1px;
            border-bottom: solid #ddd 1px;
        }

        th {
            background-color: #ddd;
        }
    </style>
</head>
<body>

<div style="width: 500px">
    <div ref="table" class="table">
        <div style="width: calc(100% - 17px);overflow-x: hidden;" id="table-head">
            <table style="background-color: #fff;width: 100%;table-layout: fixed;">
                <thead>
                <tr>
                    <th style="width: 200px">姓名</th>
                    <th style="width: 200px">性别</th>
                    <th style="width: 200px">省份证号</th>
                    <th style="width: 200px">省份证号</th>
                    <th style="width: 200px">省份证号</th>
                </tr>
                </thead>
            </table>
        </div>
        <div style="width: 100%;overflow-x: scroll;height: 150px;overflow-y: scroll;" id="table-body">
            <table style="background-color: #fff;width: 100%;table-layout: fixed">
                <tbody>
                <tr>
                    <td style="width: 200px">张三开始</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">
                        <img src="//www.baidu.com/img/bd_logo1.png" style="height: 28px;width: auto">
                        <img src="//www.baidu.com/img/bd_logo1.png" style="height: 28px;width: auto">
                        <img src="//www.baidu.com/img/bd_logo1.png" style="height: 28px;width: auto">
                        <img src="//www.baidu.com/img/bd_logo1.png" style="height: 28px;width: auto">
                        <img src="//www.baidu.com/img/bd_logo1.png" style="height: 28px;width: auto">
                    </td>
                </tr>
                <tr>
                    <td style="width: 200px">张三1</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三2</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                <tr>
                    <td style="width: 200px">张三结束</td>
                    <td style="width: 200px"></td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                    <td style="width: 200px">xxxxxxxxxxxxxxxxxxx</td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script src="../../../src/lib/jquery/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    var $tableHead = $('#table-head');
    var $tableBody = $('#table-body');
    /*设置同步横向滚动*/
    $tableBody.scroll(function (ev) {
        $tableHead.scrollLeft($tableBody.scrollLeft()); // 横向滚动条
    });

</script>
</body>
</html>


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Bootstrap 中,可以使用固定表头滚动表体的插件来实现这个效果。以下是实现步骤: 1. 引入必要的文件: ``` <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> ``` 2. 创建一个格,并在格头部添加 `thead` 标签: ``` <table class="table table-bordered table-striped"> <thead> <tr> <th>列头1</th> <th>列头2</th> <th>列头3</th> </tr> </thead> <tbody> <tr> <td>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</td> </tr> <!-- more rows --> </tbody> </table> ``` 3. 使用 JavaScript 初始化格,并调用插件: ``` $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); ``` 其中,`fixedHeaderTable` 是插件名称,`fixedColumn: true` 示需要固定表头。 4. 根据需要调整样式: ``` .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } ``` 其中,`.table-fixed-header` 是插件自动生成的类名,可以根据需要进行修改。 完整代码如下: ``` <!DOCTYPE html> <html> <head> <title>Bootstrap Table 表头固定表体滚动</title> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <style> .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } </style> </head> <body> <div class="container"> <table class="table table-bordered table-striped table-fixed-header"> <thead> <tr> <th>列头1</th> <th>列头2</th> <th>列头3</th> </tr> </thead> <tbody> <tr> <td>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</td> </tr> <!-- more rows --> </tbody> </table> </div> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fixed-header-table@1.3.2/dist/fixed-header-table.min.js"></script> <script> $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); </script> </body> </html> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值