laravel dca-admin grid表格的表头固定如何实现
可以使用一些CSS技巧来实现表头固定,例如使用CSS的position属性将表头固定在页面顶部,或者使用JavaScript来实现表头跟随滚动条滚动。以下是一些示例代码:
- 使用CSS position属性实现表头固定
<div class="table-container">
<table class="table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
...
</tbody>
</table>
</div>
<style>
.table-container {
overflow-x: auto;
max-width: 100%;
position: relative;
}
.table {
width: max-content;
}
thead th {
position: sticky;
top: 0;
background: #fff;
}
</style>
- 使用JavaScript实现表头跟随滚动条滚动
<div class="table-container">
<table class="table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
...
</tbody>
</table>
</div>
<script>
const tableContainer = document.querySelector('.table-container');
const table = document.querySelector('.table');
const tableHeader = table.querySelector('thead');
tableContainer.addEventListener('scroll', () => {
tableHeader.style.transform = `translateY(${tableContainer.scrollTop}px)`;
});
</script>
原文地址: https://www.cveoy.top/t/topic/bXWE 著作权归作者所有。请勿转载和采集!