简体   繁体   English

滚动时始终在顶部的高表单元格中显示文本

[英]Always display text in high table cells on top when scrolling

In my project I have a table.在我的项目中,我有一张桌子。 In the leftmost column groups are displayed;在最左边的列中显示组; on the right the details.在右边的细节。 Sometimes there can be many entries in a group, so that the group may not fit completely on the screen.有时一个组中可能有许多条目,因此该组可能无法完全显示在屏幕上。 Since the text in the left column is displayed at the top, it is possible that the label disappears when scrolling.由于左栏中的文本显示在顶部,因此滚动时 label 可能会消失。

I've added an example picture for you.我为您添加了示例图片。 The red area represents the viewport.红色区域代表视口。 If you scroll down, Group 1 should stay visible until the user scrolls down further.如果向下滚动,组 1应该保持可见,直到用户进一步向下滚动。

Is there a way to pin the text to the top of the screen using CSS or JavaScript in such a way that it stays at the top while scrolling, as long as the cell is still in the viewport?有没有办法使用 CSS 或 JavaScript 将文本固定到屏幕顶部,只要单元格仍在视口中,它就会在滚动时保持在顶部?

例子

I found a solution.我找到了解决方案。 First, you have to put the group labels into a '' element.首先,您必须将组标签放入 '' 元素中。 Then add bootstrap's 'sticky-top' class, or the following attributes:然后添加引导程序的'sticky-top' class,或以下属性:

.sticky-top {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}

An example would be:一个例子是:

<table>
  <tr>
    <th rowspan="2">
      <span class="sticky-top">Group</span>
    </th>
    <td>Detail</td>
  </tr>
  <tr>
    <td>Detail</td>
  </tr>
</table>

Using "position: sticky;"使用“位置:粘性;” can help you, I guess.可以帮助你,我猜。

 div.sticky { position: -webkit-sticky; /* Safari */ position: sticky; top: 0; }
 <div> <div class="sticky">Im sticky!</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM