位置粘在头上

2023-12-05

你可能知道,position: sticky;已登陆Webkit(demo)。 到目前为止,我可以看到这只适用于父元素。但我想知道是否可以在带有表格的滚动 div 中使用它。

所以它需要“监听”的滚动事件div,不是table.

我知道我可以使用 javascript 和绝对定位来做到这一点,但我想知道是否sticky-positioning会支持这一点。


位置粘在thead th2018年工作!

在您的样式表中只需添加这一行:

thead th { position: sticky; top: 0; }

您的表格需要包括thead and th为了这个风格。

<table>
    <thead>
        <tr>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
            <th>column 4</th>            
        </tr>    
    </thead>
    <tbody>
      // your body code
    </tbody>
</table>

另外,如果您有多行thead,您可以选择第一个保持粘性:

thead tr:first-child th { position: sticky; top: 0; }

截至 2018 年 3 月,现代浏览器几乎都支持 参考:https://caniuse.com/#feat=css-sticky

这归功于@ctf0(参考评论于 2017 年 12 月 3 日发表)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

位置粘在头上 的相关文章