CSS实现Table多表头与列固定

CSS   2024-07-20 11:23   75   0  
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>css实现table多表头与列固定</title>
  <style>
    .wrap {
      height: 700px;
      width: 700px;
      overflow: auto;
    }
    .table {
      border-collapse: collapse;
    }
    .table td, .table th {
      border: 1px solid #ccc;
      padding: 4px 8px;
      white-space: nowrap;
    }

    /* 固定表头 */
    .table thead th {
      z-index: 2;
      background: #e3e5e8;
      filter: drop-shadow(0 -1px 0 #ccc)
              drop-shadow(1px 0 0 #ccc)
              drop-shadow(0 1px 0 #ccc)
              drop-shadow(-1px 0 0 #ccc);
    }
    .table thead {
      position: sticky;
      top: 0;
      /* 高于固定列是td的z-index防止,td压盖th */
      z-index: 2; 
    }

    /* 固定列 */
    .table tbody td {
      z-index: 1;
      background: #fff;
    }

    .table .fix-left {
      position: sticky;
      left: 0;
      filter: drop-shadow(0 -1px 0 #ccc)
              drop-shadow(1px 0 0 #ccc)
              drop-shadow(0 1px 0 #ccc)
              drop-shadow(-1px 0 0 #ccc);
    }

    .table td:nth-child(2).fix-left,
    .table th:nth-child(2).fix-left {
      /* 92包括下面右边102类似魔术值,当前还未想到如何不适用js来实现这一效果 */
      left: 92px;
    }

    .table .fix-right {
      position: sticky;
      /* 注意这里是1px,不是0,因为table与父容器之间有1px间隙,设置为0时会出现轻微的移动 */
      right: 1px;
      filter: drop-shadow(0 -1px 0 #ccc)
              drop-shadow(1px 0 0 #ccc)
              drop-shadow(0 1px 0 #ccc)
              drop-shadow(-1px 0 0 #ccc);
    }

    .table td:nth-last-child(2).fix-right,
    .table th:nth-last-child(2).fix-right {
      right: 102px;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <table class="table" id="resizeMe">
      <thead id="thead"></thead>
      <tbody id="tbody"></tbody>
    </table>
  </div>
  
  <!-- 创建表格内容 -->
  <script>
    const thead = document.getElementById('thead')
    const thr = document.createElement('tr')

    const tbody = document.getElementById('tbody')
    // 表头
    for (let i = 0; i < 12; i++) {
      const th = document.createElement('th')
      th.innerText = `column ${i + 1}`
      if ([0, 1].includes(i)) {
        th.className = 'fix-left'
      }
      if ([10, 11].includes(i)) {
        th.className = 'fix-right'
      }
      thr.appendChild(th)
    }
    thead.appendChild(thr)
    // 多表头
    const thr2 = document.createElement('tr')
    for (let i = 0; i < 6; i++) {
      const th = document.createElement('th')
      th.innerText = `column ${i + 1}`
      th.colSpan = 2
      if ([0].includes(i)) {
        th.className = 'fix-left'
      }
      if ([5].includes(i)) {
        th.className = 'fix-right'
      }
      thr2.appendChild(th)
    }
    thead.appendChild(thr2)

    for (let i = 0; i < 24; i++) {
      const tbr = document.createElement('tr')
      for (let j = 0; j < 12; j++) {
        const td = document.createElement('td')
        td.innerText = `内容 ${i + 1}-${j + 1}`
        if ([0, 1].includes(j)) {
          td.className = 'fix-left'
        }
        if ([10, 11].includes(j)) {
          td.className = 'fix-right'
        }
        tbr.appendChild(td)
      }
      tbody.appendChild(tbr)
    }
  </script>
</body>
</html>