table 固定前两行

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        table {
            width: 100%;
            text-align: center;
            border-collapse: collapse;
            border-spacing: 0;
        }

            table td {
                word-break: break-all;
                word-wrap: break-word;
            }

        .container {
            width: 600px;
            height: 500px;
            padding: 0;
            box-sizing: border-box;
        }

        #left-div {
            width: 80px;
            float: left;
        }

        #left-div1 {
            width: 100%;
        }

        #left-div2 {
            width: 100%;
            height: 250px;
            overflow: hidden;
        }

        #left-table2 {
            margin-bottom: 4px;
        }

        #right-div {
            float: left;
            width: 240px;
        }

        #right-div1 {
            width: 100%;
            overflow: hidden;
        }

        #right-div2 {
            width: 100%;
            height: 250px;
            overflow: auto;
        }

        #right-table1 {
            width: 320px;
        }

        #right-table2 {
            width: 320px;
            overflow: auto;
        }

        th, td {
            height: 50px;
            width: 80px;
            line-height: 50px;
            overflow: hidden;
            text-align: center;
        }

        th {
            color: #1E304F;
            background-color: #F3F8FF;
        }

        td {
            color: #384967;
        }

        tr:nth-of-type(odd) {
            background: #E7F2FF;
        }
        /*可以美化一下滚动条*/
        #right-div2::-webkit-scrollbar { /*滚动条整体样式*/
            width: 4px;
            height: 4px;
        }

        #right-div2::-webkit-scrollbar-thumb { /*滚动条里面小方块*/
            border-radius: 5px;
            box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            background: rgba(0,0,0,0.2);
        }

        #right-div2::-webkit-scrollbar-track { /*滚动条里面轨道*/
            box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            border-radius: 0;
            background: rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="left-div">
            <div id="left-div1">
                <table>
                    <tr>
                        <th>编号</th>
                    </tr>
                </table>
            </div>
            <div id="left-div2">
                <table id="left-table2"></table>
            </div>
        </div>
        <div id="right-div">
            <div id="right-div1">
                <table id="right-table1">
                    <tr>
                        <th>设备名称</th>
                        <th>设备类型</th>
                        <th>故障类型</th>
                        <th>故障状态</th>
                    </tr>
                </table>
            </div>
            <div id="right-div2">
                <table id="right-table2"></table>
            </div>
        </div>
    </div>
    <script src="js/jquery-1.11.3.js"></script>
    <script type="text/javascript">
        //生成表格内容
        let htmlLeft = '';
        let htmlRight = '';
        for (let i = 1; i <= 7; i++) {
            htmlLeft += '<tr>';
            htmlLeft += '<td>' + i + '</td>';
            htmlLeft += '</tr>';
        }
        for (let i = 1; i <= 7; i++) {
            htmlRight += '<tr>';
            htmlRight += '<td>A</td>';
            htmlRight += '<td>100</td>';
            htmlRight += '<td>500</td>';
            htmlRight += '<td>1</td>';
            htmlRight += '</tr>';
        }
        $('#left-table2').html(htmlLeft);
        $('#right-table2').html(htmlRight);
        //滚动
        $('#right-div2').on('scroll', function () {
            let top = $(this).scrollTop();
            let left = $(this).scrollLeft();
            $('#left-div2').scrollTop(top);
            $('#right-div1').scrollLeft(left);
        })
    </script>
</body>
</html>

### 实现表格中固定两行的方法 为了实现在HTML中的`table`标签内固定两行的效果,CSS样式起着至关重要的作用。通过设置特定的选择器和属性组合,能够达到预期效果。 对于希望固定的头部部分,即这里的两行,应该将其放置于`<thead>`标签内部[^2]。而对于表格主体,则继续使用`<tbody>`标签包裹其余行项。这样做的好处在于不仅逻辑清晰,而且有助于后续应用不同的样式规则区分处理。 针对具体的样式定义,在CSS文件或页面内的<style>区块里加入如下代码片段: ```css /* 设置整个表格的布局模式为定宽 */ table { table-layout: fixed; width: 100%; } /* 防止滚动条影响到表头宽度的一致性 */ th, td { overflow: hidden; white-space: nowrap; } /* 关键:使thead具有溢出可见特性并保持位置不变 */ thead th:nth-child(-n+2) { position: sticky; top: 0; /* 如果有多个需要固定的行,每新增一行增加相应的像素值 */ } ``` 上述方法适用于现代主流浏览器,并能较好地支持跨平台展示需求。值得注意的是,`position:sticky`这一属性并非所有旧版本浏览器都兼容良好;因此如果项目涉及较低版本IE的支持,则需考虑采用其他替代方案,比如借助JavaScript库来模拟相同行为。 #### 示例代码 下面给出一段完整的HTML与CSS配合使用的例子,用于说明如何创建一个带有固定两行的表格结构: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Fixed Top Two Rows Table</title> <style> .fixed-header-table{ table-layout:fixed;width:100%;border-collapse:collapse;border-spacing:0; } .fixed-header-table thead tr:first-child th,.fixed-header-table thead tr:nth-child(2) th{ background-color:#f7f7f7; position:-webkit-sticky;/* Safari */ position:sticky; top:0;z-index:1;top:0px; } .fixed-header-table tbody td{height:30px;} .fixed-header-table thead tr:nth-child(2) th{top:30px;} </style> </head> <body> <table class="fixed-header-table"> <thead> <tr><th colspan="4">Header Row One (Fixed)</th></tr> <tr><th>Column A</th><th>Column B</th><th>Column C</th><th>Column D</th></tr> </thead> <tbody> <!-- 假设这里有很多数据行 --> <tr><td>Data Cell</td><td>Data Cell</td><td>Data Cell</td><td>Data Cell</td></tr> ... </tbody> </table> </body> </html> ``` 此段代码展示了怎样利用简单的HTML标记加上少量CSS就能构建出拥有固定顶部两行特性的响应式表格组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值