【问题标题】:How can I make a table body scroll?如何制作表体滚动?
【发布时间】:2021-06-24 03:56:56
【问题描述】:

我有一张桌子,如果tbody 大于 大于 1000 像素,我会尝试让其滚动。

我尝试了以下方法:

.fixed_header tbody{
    display:block;
    overflow:auto;
    height:1000px;
    width:100%;
}

.fixed_header thead tr{
    display:block;
}

卷轴有效,但它破坏了我的风格。

之前

之后

实时结果: https://www.bunlongheng.com/baby/1?code=rithys4k&date=2021-03-01

如何保持滚动但让页面的其余部分保持固定?


代码

<table class="table fixed_header">

    <thead class="thin-border-bottom">
        <tr style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
            <th>Type</th>
            <th>Time</th>
            <th>Note</th>
        </tr>
    </thead>

    <tbody class="tbody-log">

        <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
            <td>
                <a class="btn btn-feed">
                    <img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924">
                </a>
            </td>
            <td>
                <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">
                01:59 am
                </a>
                <div class="modal fade" id="edit_446">
                    <div class="model-content" style="margin-top: 200px;">
                        <div class="col-sm-offset-4 col-sm-2 col-md-offset-5 col-md-2 text-center">
                            <img width="80" src="/assets/be/img/baby/feed.png">
                            <br><br><br>
                            <input type="text" value="01:59:50" name="updatedAt" width="100%" height="80">
                            <br><br>
                            <button onclick="updateLog(446)" class="btn btn-success btn-block">Done</button>
                            <br>
                            <button onclick="deleteLog(446)" class="btn btn-danger btn-block">Delete</button>
                        </div>
                    </div>
                </div>
            </td>
            <td id="td-446">
                <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
            </td>
        </tr>
        
    </tbody>
    
</table>


编辑

移动设备的目标是让内容具有完整的宽度,而不是在表格下方留出空间。

【问题讨论】:

标签: jquery css twitter-bootstrap html-table


【解决方案1】:

我想我有一个解决方案:

  • 您必须将 'tbody' 设置为 100vh 块元素并溢出。
  • 要隐藏滚动条,请仅使用 overflow-y:scroll 和 box-sizing:content-box 和 padding-right。
  • 要对齐主体单元格和头部单元格,请为它们设置宽度和文本对齐。
  • head-cells 需要是 inline-blocks,但不是 head-row 'tr' - 不要将它们设置为 display:block。
  • 由于您的表格已经以 1000px 宽度向下折叠并且整个页面都可以滚动,因此您不需要可滚动的“tbody”并且可以将相关的 css 代码包装在媒体查询中,这样它只会影响 html 1000 像素宽度。
@media screen and (min-width: 1000px) {

    .table.fixed_header>tbody {
        display: block;
        box-sizing: content-box;
        height: 100vh;
        width: 100%;
        padding-right: 32px;
        overflow-y: scroll;
    }

    .table.fixed_header>thead>tr>th {
        display: inline-block;
        width: 30%;
        text-align: center;
    }

    .table.fixed_header>tbody>tr>td {
        width: 30%;
        text-align: center;
    }

}

工作示例(宽度较小,因为 sn-p 窗口小):

#table-wrapper {
  height: 100vh;
  width: 50%;
  overflow: hidden;
}

.table.fixed_header {
  width: 100%;
}

@media screen and (min-width: 400px) {

    .table.fixed_header>tbody {
      display: block;
      box-sizing: content-box;
      height: 100vh;
      width: 100%;
      padding-right: 32px;
      overflow-y: scroll;
    }

    .table.fixed_header>thead>tr>th {
      display: inline-block;
      width: 30%;
      text-align: center;
    }

    .table.fixed_header>tbody>tr>td {
      width: 30%;
      text-align: center;
    }

}
<div id="table-wrapper">
  <div id="linkBox">
    <a>some link,</a>
    <a>another link</a>
  </div>
  <table class="table fixed_header">

    <thead class="thin-border-bottom">
      <tr style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <th>Type</th>
        <th>Time</th>
        <th>Note</th>
      </tr>
    </thead>

    <tbody class="tbody-log">

      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>
      <tr data-type="feed" style="border-top: 0px; border-bottom: 0px; box-shadow: none;">
        <td>
          <a class="btn btn-feed"><img class="logIconSmall" src="/assets/be/img/baby/feed.png?q=0.05347300 1616851924"></a>
        </td>
        <td>
          <a data-toggle="modal" data-target="#edit_446" data-backdrop="static" data-keyboard="false" type="button" class="btn btn-link">01:59 am</a>
        </td>
        <td id="td-446">
          <span style="font-weight: bold; color:#00ff5c;">+36955.2m </span>
        </td>
      </tr>

    </tbody>

  </table>
</div>

【讨论】:

  • 桌面似乎可以工作,但在手机上,底部显示了很多额外的代码。在电话上看到这个:bunlongheng.com/baby/1?code=rithys4k&date=2021-04-02 它有你的代码。
  • 我在我的安卓手机上的 4 个浏览器和我的 windows 敞篷车上的 4 个浏览器中测试了该网站(也在开发人员工具中的移动视图中)。但我没有看到额外的代码 - 没有在底部,也没有在任何其他地方。只有我在桌面浏览器中看到的元素。我发现的唯一错误是 33% 的宽度导致在 Firefox(移动和桌面)中表头在第二行中换行。您可以通过将“th”和“tr”的宽度设置为 33% 到 30% 来解决此问题。你看到的额外代码是什么意思?会不会是错误信息?
  • 我很抱歉我用错了词。我的意思是在移动设备上,我看到了多余的开放空间。
  • 再次抱歉我的错字*,请看这个:i.imgur.com/9H0Bb3U.jpg 它不会使用整个宽度,并在底部显示很多额外的空间。
  • 当我注释掉你的代码时,我没有看到死区并且它占据了全宽:i.imgur.com/d4Qzm7J.jpg
猜你喜欢
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2012-08-02
  • 2021-05-18
  • 1970-01-01
  • 2013-01-25
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多