gpt4 book ai didi

jquery - 添加水平滚动表

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:20 24 4
gpt4 key购买 nike

我创建了一个简单的 HTML 表格,其中一些数据是预定义的,但我想在此表格中添加水平滚动,其中 Top Row(Name, Title) 将始终固定。但是如何添加水平滚动条呢?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body>
<table width="200" border="1">
<tr>
<td><b>Name</b></td>
<td><b>Title</b></td>
</tr>
<tr>
<td>Atish</td>
<td>kumar</td>
</tr>
<tr>
<td>Sandip</td>
<td>Das</td>
</tr>
<tr>
<td>Rohit</td>
<td>Sen</td>
</tr>
<tr>
<td>Atul</td>
<td>Sharma</td>
</tr>
<tr>
<td>Moumita</td>
<td>Sen</td>
</tr>
<tr>
<td>Nirmal</td>
<td>Roy</td>
</tr>
<tr>
<td>Susmita</td>
<td>Chatterjee</td>
</tr>
<tr>
<td>Sekhar</td>
<td>Das</td>
</tr>
<tr>
<td>Gour</td>
<td>Sen</td>
</tr>
<tr>
<td>Jotin</td>
<td>Roy</td>
</tr>
<tr>
<td>Shyam</td>
<td>Das</td>
</tr>
<tr>
<td>Dipak</td>
<td>Das</td>
</tr>
<tr>
<td>Partha</td>
<td>Sen</td>
</tr>
</table>

</body>
</html>

最佳答案

尝试使用 CSS

.scrollableTable {
width: 100%;
}
.scrollableTable thead {
display: block;
}
.scrollableTable thead th:nth-child(1) {
width: 30%;
}
.scrollableTable thead th:nth-child(2) {
width: 30%;
}
.scrollableTable tbody {
display: block;
overflow-y: scroll;
width: 100%;
height: 200px;
}
.scrollableTable tbody tr {
display: block;
width: 100%;
}
.scrollableTable tbody tr td {
display: inline-block;
}
.scrollableTable tbody tr td:nth-child(1) {
/* Calculate ((100% - scrollbar width) * ( % / 100)) to get proper column percent */
width: calc((100% - 11px) * (20 / 100));
}
.scrollableTable tbody tr td:nth-child(2) {
/* Calculate ((100% - scrollbar width) * ( % / 100)) to get proper column percent */
width: calc((100% - 11px) * (80 / 100));
}
<table class="scrollableTable">
<thead>
<tr>
<th><b>Name</b></th>
<th><b>Title</b></th>
</tr>
</thead>
<tbody>
<tr>
<td>Atish</td>
<td>kumar</td>
</tr>
<tr>
<td>Sandip</td>
<td>Das</td>
</tr>
<tr>
<td>Rohit</td>
<td>Sen</td>
</tr>
<tr>
<td>Atul</td>
<td>Sharma</td>
</tr>
<tr>
<td>Moumita</td>
<td>Sen</td>
</tr>
<tr>
<td>Nirmal</td>
<td>Roy</td>
</tr>
<tr>
<td>Susmita</td>
<td>Chatterjee</td>
</tr>
<tr>
<td>Sekhar</td>
<td>Das</td>
</tr>
<tr>
<td>Gour</td>
<td>Sen</td>
</tr>
<tr>
<td>Jotin</td>
<td>Roy</td>
</tr>
<tr>
<td>Shyam</td>
<td>Das</td>
</tr>
<tr>
<td>Dipak</td>
<td>Das</td>
</tr>
<tr>
<td>Partha</td>
<td>Sen</td>
</tr>
</tbody>
</table>

水平滚动

.table-wrapper { 
overflow-x:scroll;
overflow-y:visible;
width:250px;
margin-left: 120px;
}


td, th {
padding: 5px 20px;
width: 100px;
}

th:first-child {
position: fixed;
left: 5px
}
<div class="table-wrapper">
<table id="consumption-data" class="data">
<thead class="header">
<tr>
<th><b>Name</b></th>
<td>Atish</td>
<td>Sandip</td>
<td>Rohit</td>
<td>Atul</td>
<td>Moumita</td>
<td>Nirmal</td>
<td>Susmita</td>
<td>Sekhar</td>
<td>Gour</td>
<td>Jotin</td>
<td>Shyam</td>
<td>Dipak</td>
<td>Partha</td>
</tr>
</thead>
<tbody class="results">
<tr>
<th>Title</th>
<td>kumar</td>
<td>Das</td>
<td>Sen</td>
<td>Sharma</td>
<td>Sen</td>
<td>Roy</td>
<td>Chatterjee</td>
<td>Das</td>
<td>Sen</td>
<td>Roy</td>
<td>Das</td>
<td>Das</td>
<td>Sen</td>
</tr>
</tbody>
</table>
</div>

关于jquery - 添加水平滚动表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44668598/

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