javascript - 如何使 <tr> 充当固定的顶部栏?

标签 javascript jquery html css

我有一个显示一些信息的表格,第一行是相应信息的标题。

滚动时,假设距离页面顶部 100 像素,我希望标题行固定在可见屏幕的顶部,这样当您向下滚动表格时,您仍然可以看到标题。

我试过:

$(window).scroll(function(){
    if( $(window).pageYOffset > 100 ){
        $("#resultTableHeader").css('position', 'fixed');
        $("#resultTableHeader").css('top', '70px');
        $("#resultTableHeader").css('z-index', '2');
    }
});

但它似乎没有做任何事情。

即使这样做,但是,使标题行固定,如果一列扩展了标题行列,那么将其从流中拉出将使该列不再扩展。

有办法吗?

编辑(更多信息):

我可以保证整个页面将只是表格,因此对于任何超过 100 像素的 Y 坐标,停留在屏幕顶部是可以接受的。 当我说固定时,我的意思是当您沿着表格向下滚动时,标题行将保持固定在 VISIBLE 窗口的顶部。

最佳答案

用纯html和css固定表头

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;
}
<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>

关于javascript - 如何使 <tr> 充当固定的顶部栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302072/

相关文章:

javascript - 包含 Node.JS (vue) 的文件

javascript - 循环遍历具有相似类的元素

javascript - 在一定量后调整大小时刷新浏览器

javascript - 通过一次表单提交将多个文件上传到一个文件夹中

php - 使用javascript动态计算总和

javascript - Svg 圆形填充 CSS

javascript - 初始化可观察数组时传递的参数必须是数组,或者为 null,或者为 undefined

jQuery 从特定行选择文本

Jquery点击点变div

javascript - 使用 JAVASCRIPT 单击菜单链接时添加事件类