【问题标题】:How to add scrollbar only to tbody and not fixed thead如何仅将滚动条添加到 tbody 而不是固定 thead
【发布时间】:2020-01-16 09:46:45
【问题描述】:

我正在尝试创建一个具有可滚动<tbody> 和固定<thead> 的表。我在这里实现了https://jsfiddle.net/ghnfzwm6/16/

但是,这并不是我想要的。我希望滚动条从 <tbody> 开始的地方开始。我试图通过添加来做到这一点

height: 546px;
max-height: 546px;
overflow-y: scroll; /* only Y axis scroll */

<tbody> 并将其从#tasks-table-wrapper 中删除。我还做了<tbody><tr>display: block;

这是结果https://jsfiddle.net/4gLwp697/3/。现在的问题是<thead><tbody> 不一致。我可以通过指定列宽来解决这个问题,但我希望它们是自动的。

我该如何解决这个问题?

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    考虑这段代码sn-p,没有直接解决你的问题,这是由'table'特性决定的。目前主流框架的解决方案如下,需要设置'columns'的宽度。

    * {
      margin: 0;
      padding: 0;
    }
    
    #content {
      overflow: hidden;
      height: 100%;
      text-align: center;
    }
    
    #tasks-table-wrapper {
      text-align: left;
      display: inline-block;
      width: auto;
    }
    .scrollbar-wrap {
      overflow: auto;
      height: 100px;
    }
    .black-table {
      border-collapse: separate;
      font-size: 0;
      background-color: #121212;
      text-transform: uppercase;
      border-spacing: 0;
    }
    
    .black-table td {
      color: #bbb;
      font-size: 11px;
      letter-spacing: 1px;
      padding: 15px 30px;
    }
    .black-table th {
      color: #fff;
      font-size: 12px;
      font-weight: 700;
      letter-spacing: 2px;
      padding: 15px 25px;
      border-bottom: 1px solid #1c1c1c;
      position: sticky; /* keep TH on top */
      top: 0;
      background-color: #121212;
      white-space: nowrap;
    }
    <div id="content">
      <div id="tasks-table-wrapper">
        <table class="black-table">
            <colgroup>
              <col width="180">
              <col width="180">
              <col width="180">
              <col width="180">
            </colgroup>
          <thead>
            <tr>
              <th>Column 1</th>
              <th>Column 2</th>
              <th>Column 3</th>
              <th>Column 4</th>
            </tr>
          </thead>
        </table>
        <div class="scrollbar-wrap">
          <table class="black-table">
            <colgroup>
              <col width="180">
              <col width="180">
              <col width="180">
              <col width="163">
            </colgroup>
            <tbody>
              <tr>
                <td>AAA</td>
                <td>BBB</td>
                <td>CCC</td>
                <td>DDD</td>
              </tr>
              <tr>
                <td>AAA</td>
                <td>BBB</td>
                <td>CCC</td>
                <td>DDD</td>
              </tr>
              <tr>
                <td>AAA</td>
                <td>BBB</td>
                <td>CCC</td>
                <td>DDD</td>
              </tr>
              <tr>
                <td>AAA</td>
                <td>BBB</td>
                <td>CCC</td>
                <td>DDD</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    可以看到VUE Table with fixed header,查看表格元素。

    【讨论】:

    • 表格中的内容会发生变化,所以不幸的是,这对我来说并没有解决任何问题。
    • @Jack P 'JavaScript' 的解决方案是将你的'table' 分成两个独立的部分,我已经尝试过你之前提出的问题,并且我查阅了很多信息。上述方案是大多数框架使用的方案。
    • JavaScript 解决方案是计算每个&lt;th&gt; 和每一列的宽度,取其中最大的一个并将列宽设置为那个值。
    • @Jack P 你可以这样想,但是,使用这种方法需要你考虑滚动条的宽度,在我看来,这个方案比较麻烦。但是没有更好的解决方案。
    【解决方案2】:

    替换

    #tasks tr {
      display: block;
    }
    

    #tasks tr {
      display: table;
      width: 100%;
      table-layout: fixed;
    }
    

    https://jsfiddle.net/5nt18cL0/

    【讨论】:

    • @JackP 没错,我错了,因为示例数据都是 3 个字符。
    • @JackP 貌似通过添加table-layout: fixed;,它可以工作?
    • 什么都没有改变?仍然未对齐prntscr.com/qobj9c
    • 哦,我明白了,当有更多内容时,您已修复它移动。很好,但仍然没有正确排列。
    • @JackP 为tbody 添加width: calc(100% + scrollbarwidth) 怎么样? jsfiddle.net/5n07awcs (或者#tasks tbody tr,如果你想保持滚动条可见)
    猜你喜欢
    • 2018-05-23
    • 2012-09-28
    • 1970-01-01
    • 2023-03-24
    • 2019-06-12
    • 2016-05-07
    • 2018-04-06
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多