【问题标题】:Horizontal scrolling on table with fixed header具有固定标题的表格上的水平滚动
【发布时间】:2017-08-10 02:36:35
【问题描述】:

我已经使用 CSS 创建了一个固定的标题(主要只是设置要固定的头的位置),但我遇到的问题是如果用户的分辨率大小或窗口大小小于表格大小,我需要添加一个水平滚动条,以便他们可以看到所有内容。我尝试将溢出设置为自动和滚动,但只有当我向下滚动到页面底部时才会出现滚动条。此外,我需要能够让标题随表格滚动。如果窗口小于表格大小,是否有关于显示水平滚动条的任何建议,以及如何让标题仍然固定在一个位置,但如果窗口尺寸太小,仍然滚动以查看更多标题?

http://jsfiddle.net/E9mqk/1/

HTML 是:

<div class="table-wrapper">
<table id="table-information">
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <thead class="table-fixed-header" id="table-data">
        <tr>
            <th>Something 1</th>
            <th>Something 2</th>
            <th>Something 3</th>
            <th>Something 4</th>
            <th>Something 5</th>
            <th>Something 6</th>
            <th>Something 7</th>
            <th>Something 8</th>
        </tr>
    </thead>
    <tbody class="table-body">   
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
    </tbody>
</table>
</div>

CSS 是:

.table-wrapper {
    overflow-x: scroll;
}
.table-fixed-header {
    position: fixed;
}
#table-information tbody:before {
    line-height: 30px;
    content:"-";
    color:white; /* to hide text */
    display:block;
}
#table-information td {
    max-width: 100px;
    min-width: 100px;
}
#table-information th {
    max-width: 100px;
    min-width: 100px;
}

【问题讨论】:

标签: css scroll html-table scrollbar


【解决方案1】:

假设你没有在 Javascript 中做一些会在表格上产生奇怪行为的东西,你可以把表格包装在一个

 <div style="overflow-x: scroll;">
   [your table here]
 </div>

或其他具有相同样式的块元素,您将获得您所寻求的行为,包括头部和身体相互滚动 - 请参阅 this jsfiddle 以获取工作示例。

【讨论】:

  • 当然,如果你的问题不是你不能让滚动条出现,而是你不能让滚动条出现在桌子下方以外的任何地方,那就改变了 - - 最好的建议是,在 Javascript 中,在容器 div 上设置一个小于用户的 window.innerHeight 的高度和最大高度,并设置溢出-y:滚动;在容器 div 上,以便用户可以双向滚动以查看所有表格。我承认,这不是一个很好的解决方案;如果可能的话,我建议你完全做其他事情,但如果你不能,这可能会让你继续前进。
  • 是的,我看到只有当我将容器 div 的高度设置为小于窗口大小时,才会始终看到滚动条。所以我可能不得不使用 js 做一些事情。关于固定标题的任何想法?让它与表格一起滚动?
  • 你不应该做任何特别的事情来让thead随着表格滚动——thead存在于表格元素中,所以如果你在整个表格周围滚动一个容器,它应该把thead和它一起带上,就像tbody一样。您能否发布一个指向您正在使用的示例的链接,以便我了解发生了什么?
  • 我添加了 jsfiddle。如您所见,正文滚动,但标题不滚动。
  • 为什么需要位置:固定;在表头上?删除它允许标题与正文一起滚动,我不确定首先将它放在那里有什么好处。我已经相应地编辑了jsfiddle;看看,如果能解决问题,请告诉我。
猜你喜欢
  • 2017-09-24
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 2016-10-11
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
相关资源
最近更新 更多