gpt4 book ai didi

javascript - 我们如何使用修复标题修复滚动并调整标题宽度以及 td 相等?

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:16 24 4
gpt4 key购买 nike

如果我添加 n 个标题,它将设置为一定的最小宽度,并且如果标题宽度的总最小宽度超过必须提供的表格宽度滚动的 100%,则同时希望 td 宽度等于标题宽度:

html代码如下: https://jsfiddle.net/1w7vnLec/

<html>
<head>
<style>
table.scroll {
width: 100%;
/* Optional */
/* border-collapse: collapse; */
border-spacing: 0;
border: 2px solid black;
}

table.scroll tbody,
table.scroll thead { display: block; }

thead tr th {
height: 30px;
line-height: 30px;
/* text-align: left; */
}

table.scroll tbody {
height: 100px;
overflow-y: auto;
overflow-x: scroll;
}

tbody { border-top: 2px solid black; }

tbody td, thead th {
/* width: 20%; */ /* Optional */
border-right: 1px solid black;
/* white-space: nowrap; */
}

tbody td:last-child, thead th:last-child {
border-right: none;
}

</style>




</head>
<body>
<table class="scroll">
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
<th>Head 6</th>
<th>Head 7</th>
<th>Head 8</th>
<th>Head 9</th>
<th>Head 10</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 1</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>

</tbody>
</table>
</body>
</html>

我们如何使用固定标题固定滚动并调整标题宽度以及 td 相等?

最佳答案

删除你的 JS 并删除这个 CSS

table.scroll tbody,
table.scroll thead {
display: block;
}

然后为您的 td 设置一个最小宽度,并将其父级设置为 scroll: overflowscroll: auto overflow -x: auto 取决于您要查找的行为。

这是一个有效的代码片段:

.scroll {
overflow: scroll;
}

table {
width: 100%;
border-spacing: 0;
border: 2px solid black;
}

thead tr th {
height: 30px;
line-height: 30px;
}

table.scroll tbody {
height: 100px;
overflow-y: auto;
overflow-x: scroll;
}

tbody {
border-top: 2px solid black;
}

tbody td,
thead th {
border-right: 1px solid black;
}

tbody td:last-child,
thead th:last-child {
border-right: none;
}

tbody td {
min-width: 200px;
}
<html>
<head>
<script src="jquery-1.9.1.min.js"></script>




</head>
<body>
<div class="scroll">
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
<th>Head 6</th>
<th>Head 7</th>
<th>Head 8</th>
<th>Head 9</th>
<th>Head 10</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 1</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
<td>Content 5</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

关于javascript - 我们如何使用修复标题修复滚动并调整标题宽度以及 td 相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55216809/

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