gpt4 book ai didi

html - 带有两个表的表固定头与 tbody 上的滚动条存在对齐问题

转载 作者:行者123 更新时间:2023-11-28 14:31:46 26 4
gpt4 key购买 nike

我添加了两个表格,使 thead 部分固定在顶部。表(tbody)中的内容来自数据库。所以,行数可能有小行数也可能有大行数。

考虑这段代码。

如果 tbody 有几行:

.tables {
padding: 20px;
}

table {
border: 1px solid #ddd;
text-align: left;
font-family: sans-serif;
font-size: 13px;
}

table th,
table td {
border: 1px solid #ddd;
padding: 2px 5px;
}

.table-head table thead tr th:last-child {
width: 15%;
}

.tables .table-body {
max-height: 200px;
overflow-y: auto;
}

.table-body table tbody tr td:last-child {
width: 15%;
}

.table-body::-webkit-scrollbar {
width: 10px;
margin-right: -10px;
position: absolute;
height: 100%;
right: -10px;
}

.table-body::-webkit-scrollbar-track {
background: #f1f1f1;
}

.table-body::-webkit-scrollbar-thumb {
background: #888;
}

.tbl-fieldList .tbl-body-only::-webkit-scrollbar-thumb:hover {
background: #555;
}
<div class="tables">
<div class="table-head">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Name</th>
<th><input type="checkbox"></th>
</tr>
</thead>
</table>
</div>

<div class="table-body">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
</tbody>
</table>
</div>
</div>

如果 tbody 有大量行,表 thtd 的对齐方式会失真:

.tables {
padding: 20px;
}

table {
border: 1px solid #ddd;
text-align: left;
font-family: sans-serif;
font-size: 13px;
}

table th,
table td {
border: 1px solid #ddd;
padding: 2px 5px;
}

.table-head table thead tr th:last-child {
width: 15%;
}

.tables .table-body {
max-height: 200px;
overflow-y: auto;
}

.table-body table tbody tr td:last-child {
width: 15%;
}

.table-body::-webkit-scrollbar {
width: 10px;
margin-right: -10px;
position: absolute;
height: 100%;
right: -10px;
}

.table-body::-webkit-scrollbar-track {
background: #f1f1f1;
}

.table-body::-webkit-scrollbar-thumb {
background: #888;
}

.tbl-fieldList .tbl-body-only::-webkit-scrollbar-thumb:hover {
background: #555;
}
<div class="tables">
<div class="table-head">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Name</th>
<th><input type="checkbox"></th>
</tr>
</thead>
</table>
</div>

<div class="table-body">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
</tbody>
</table>
</div>
</div>

我试图将自定义滚动条位置设置到 div 之外,但它不起作用。有人有解决此问题的想法吗?

最佳答案

您需要将自定义滚动条添加到“table-head”,然后添加几行 JavaScript :)

if (document.querySelector('.table-body table').offsetHeight > 200) { // same as max-height
document.querySelector('.table-head').style.overflowY = 'scroll';
}
.tables {
padding: 20px;
}

table {
border: 1px solid #ddd;
text-align: left;
font-family: sans-serif;
font-size: 13px;
}

table th,
table td {
border: 1px solid #ddd;
padding: 2px 5px;
}

.table-head table thead tr th:last-child {
width: 15%;
}

.tables .table-body {
max-height: 200px;
overflow-y: auto;
}

.table-body table tbody tr td:last-child {
width: 15%;
}

.table-body::-webkit-scrollbar,
.table-head::-webkit-scrollbar {
width: 10px;
margin-right: -10px;
position: absolute;
height: 100%;
right: -10px;
}

.table-body::-webkit-scrollbar-track,
.table-head::-webkit-scrollbar-track {
background: #f1f1f1;
}

.table-body::-webkit-scrollbar-thumb {
background: #888;
}

.tbl-fieldList .tbl-body-only::-webkit-scrollbar-thumb:hover {
background: #555;
}
<div class="tables">
<div class="table-head">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Name</th>
<th><input type="checkbox"></th>
</tr>
</thead>
</table>
</div>

<div class="table-body">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>Sample name</td>
<td><input type="checkbox"></td>
</tr>
</tbody>
</table>
</div>
</div>

关于html - 带有两个表的表固定头与 tbody 上的滚动条存在对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54105283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com