【问题标题】:position sticky for complex table header复杂表头的粘性位置
【发布时间】:2018-04-30 11:49:58
【问题描述】:

我正在尝试让粘性表格标题与复杂标题一起使用。它适用于标准表。表格将停留在第一行而不是第二行,导致标题看起来“不正常”或有点交错。我尝试将位置置顶在 thead 上,但似乎没有任何作用。

这是我的桌子:

table {
  border: 1px solid black;
  font-size: 14px;
  margin: 10px 0;
  position: relative;
}

thead tr th {
  background-color: gray;
  position: sticky;
  top: -2px;
}

thead {
  border: 1px solid black;
  line-height: 1.25;
  overflow: hidden;
}

td {
  border: 1px solid black;
  line-height: 1.25;
  overflow: hidden;
  padding: 4px;
}
<table class='sticky-table'>
  <thead>
    <tr>
      <th rowspan="2">First column</th>
      <th colspan="3">Top of second column</th>
    </tr>
    <tr>
      <th>One</th>
      <th>Two</th>
      <th>Three</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
    <tr>
      <td>First cell</td>
      <td>Second cell</td>
      <td>Third cell</td>
      <td>Fourth cell</td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: html css sticky


    【解决方案1】:

    我不喜欢这个解决方案,因为空间是“硬”编码的,但我认为它可以按需要工作,不是吗?

    table {
      border: 1px solid black;
      font-size: 14px;
      margin: 10px 0;
      position: relative;
    }
    
    thead tr th {
      background-color: gray;
      position: sticky;
      top: -2px;
    }
    
    .header {
      background-color: gray;
      position: sticky;
      top: 20px;
    }
    
    thead {
      border: 1px solid black;
      line-height: 1.25;
      overflow: hidden;
    }
    
    td {
      border: 1px solid black;
      line-height: 1.25;
      overflow: hidden;
      padding: 4px;
    }
    <table class='sticky-table'>
      <thead>
        <tr>
          <th rowspan="2">First column</th>
          <th colspan="3">Top of second column</th>
        </tr>
        <tr>
          <th class="header">One</th>
          <th class="header">Two</th>
          <th class="header">Three</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 这绝对给了我一个思考的方法。但是我不能对此进行硬编码,因为该样式必须适用于其他作者的表格。
    • 不幸的是,firefox 仍然不支持粘性表头,我认为在所有浏览器都添加适当的支持之前,使用固定位置是唯一的解决方案
    • 我只需要担心支持 css 属性的浏览器。如果不是,那么它的行为就像一张普通的桌子。
    【解决方案2】:

    相信下面的js会解决这个问题。边框有些怪异,但可以正确粘贴。

    var tableHeaderTop = document.querySelector('.sticky-table thead').getBoundingClientRect().top;
    var ths = document.querySelectorAll('.sticky-table thead th')
    
    for(var i = 0; i < ths.length; i++) {
      var th = ths[i];
      th.style.top = th.getBoundingClientRect().top - tableHeaderTop + "px";
    }
    table {
      border: 1px solid black;
      font-size: 14px;
      margin: 10px 0;
      position: relative;
    }
    
    thead tr th {
      background-color: gray;
      position: sticky;
      top: -2px;
    }
    
    thead {
      border: 1px solid black;
      line-height: 1.25;
      overflow: hidden;
    }
    
    td {
      border: 1px solid black;
      line-height: 1.25;
      overflow: hidden;
      padding: 4px;
    }
    <table class='sticky-table'>
      <thead>
        <tr>
          <th rowspan="2">First column</th>
          <th colspan="3">Top of second column</th>
        </tr>
        <tr>
          <th>One</th>
          <th>Two</th>
          <th>Three</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
        <tr>
          <td>First cell</td>
          <td>Second cell</td>
          <td>Third cell</td>
          <td>Fourth cell</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多