CSS or JavaScript (JQuery) Stickyheader for table

CSS or JavaScript (JQuery) Stickyheader for table

本文关键字:Stickyheader for table JQuery or JavaScript CSS      更新时间:2023-09-26

所以基本上,有很多粘性标头的例子。我使用了一种javascript方法,该方法适用于包含几行(100-150)的表。一旦它达到 600-700+ 行,javascript 就会继续加载,也许在 10-15 秒后你会看到你的结果。

现在你可以想象这不是安静的用户友好,因为当涉及到我的客户时,耐心是罕见的。

我对脚本运行速度较慢的原因的猜测是,它只是无法处理数据量。 脚本必须克隆表格并删除其中一个表格的头部和另一个表格的正文 - 这样就可以滚动内容并看到一个粘性标题。

总结一下并明确我的问题:创建带有粘性(浮动)标题的表的最有效方法(在速度方面有效)是什么?甚至可能只用 CSS 来做到这一点,这样我就不必使用 JS?

谢谢

诀窍

th内的div和包装器div.container

所以"棍子"标题实际上是th内的div。它们之所以粘住,是因为它们的position:absolute和滚动条属于包装器。

这绝对不是一个通用的解决方案,但它可以成为更简单情况的解决方案。

var rows = $('tbody tr');
for (var i = 0; i < 300; i++) {
	rows.clone().appendTo($('tbody'));
}
html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
section.positioned {
  position: absolute;
  top:100px;
  left:100px;
  width:800px;
  box-shadow: 0 0 15px #333;
}
.container {
  overflow-y: auto;
  height: 200px;
}
table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  padding: 10px 25px;
}
th {
  height: 0;
  line-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}
th div{
  position: absolute;
  background: transparent;
  color: #fff;
  padding: 9px 25px;
  top: 0;
  margin-left: -25px;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="">
  <div class="container">
    <table>
      <thead>
        <tr class="header">
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        <tr>
          <td>bgcolor</td>
          <td>rgb(x,x,x), #xxxxxx, colorname</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
        </tr>
        <tr>
          <td>border</td>
          <td>1,""</td>
          <td>Specifies whether the table cells should have borders or not</td>
        </tr>
        <tr>
          <td>cellpadding</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
        </tr>
        <tr>
          <td>cellspacing</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between cells</td>
        </tr>
        <tr>
          <td>frame</td>
          <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
          <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
        </tr>
        <tr>
          <td>rules</td>
          <td>none, groups, rows, cols, all</td>
          <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
        </tr>
        <tr>
          <td>summary</td>
          <td>text</td>
          <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
        </tr>
        <tr>
          <td>width</td>
          <td>pixels, %</td>
          <td>Not supported in HTML5. Specifies the width of a table</td>
        </tr>
      </tbody>
    </table>
  </div>
</section>

我不知道是谁创造了这个小提琴,但这是来源:(我添加的重复部分)

https://jsfiddle.net/dPixie/byB9d/3/light/