在 这篇文章 之后,我有一个表格,我正在尝试获取粘性标题。除了我的表格样式在顶部和底部有带边框的标题。
我不明白的部分是应用于 th
的顶部/底部边框与表格的其余部分一起滚动,而不是留在“卡住”的标题单元格中。有什么可以做的吗?
可以在此处找到工作示例: https ://codepen.io/smlombardi/pen/MNKqQY?editors=1100#0
let string = ''
console.log("oj")
for(let i=0; i<100; i++) {
string += `
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
`
}
document.getElementsByTagName('tbody')[0].innerHTML += string
.table-sticky-container {
height: 200px;
overflow-y: scroll;
border-top: 1px solid green;
border-bottom: 1px solid green;
}
.table-sticky {
th {
position: sticky;
top: 0;
z-index: 2;
border-top: 1px solid red !important;
border-bottom: 2px solid red !important;
}
}
<div class="table-sticky-container">
<table class="table table-sticky">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Title</th>
<th scope="col">ID</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
</tbody>
</table>
</div>
原文由 Steve 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以加
但不幸的是,它看起来很糟糕,更好的解决方案是使用伪元素添加变通方法。
第二种解决方案