css实现table表头和列固定功能

背景

项目需要用表格展示数据,要求表头和左边两列能够在滚动的时候固定,考虑到目前开源的table组件库冗余了太多项目不需要的功能,其次也是为了防止数据过多的时候渲染太慢,于是决定用html和css实现。

功能

表头固定
左边两类固定
高度要自适应(table默认支持)

思路

使用table的table-layout: fixed属性设置固定列宽,这样才能在左右滚动时计算出偏移量。结合css的position:sticky属性,设置第一行和左右两列粘性定位,其中第二列的偏移量根据第一列的宽度决定,以此类推,如果有三列固定,需要知道前两列的宽度。

table-layout: fixed根据MDN的描述,table-layout用于定义了用于布局表格单元格,行和列的算法,fixed属性值用于表示某一列的宽度仅由该列首行的单元格决定。因此,该单元格所在行之后的行并不会影响整个列宽。

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>

  <style>
    .table{
      overflow:auto;
      width:400px;
      height:300px; /* 固定高度 */
      border:1px solid #999;
      border-bottom: 0;
      border-right: 0;
    }


    table {
      border-collapse:separate;
      table-layout: fixed;
      width: 100%; /* 固定寬度 */
    }


    td, th {
      border-right :1px solid #999;
      border-bottom :1px solid #999;
      box-sizing: border-box;
      /* 单元格宽高 */
      width:100px;
      height:30px;
    }
    th {
      background-color:lightblue;
    }


    /* 控制左边固定的核心代码 */
    td:nth-child(1),
    th:nth-child(1) {
      position:sticky;
      left:0; /* 首行在左 */
      z-index:1;
      background-color:lightpink;
    }
    td:nth-child(2),
    th:nth-child(2) {
      position:sticky;
      left:100px;
      z-index:1;
      background-color:lightpink;
    }

    /* 控制表头固定的核心代码 */
    thead tr th {
      position:sticky;
      top:0; /* 第一列最上 */
    }

    th:nth-child(1),
    th:nth-child(2){
      z-index:2;
      background-color:lightblue;
    }
  </style>
</head>
<body>


<div class="table">
  <table cellspacing="0" border="0" cellpadding="0">

    <thead>

    <tr>

      <th>表头1</th>

      <th>表头2</th>

      <th>表头3</th>

      <th>表头4</th>

      <th>表头5</th>

    </tr>

    </thead>

    <tbody>

    <tr>

      <td ></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>
    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>
    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>
    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr><tr>

      <td>我的高度不固定我的高度不固定我的高度不固定我的高度不固定我的高度不固定我的高度不固定</td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>
    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>



    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>



    </tbody>

  </table>

</div>
</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值