gpt4 book ai didi

javascript - 垂直滚动不适用于表格

转载 作者:行者123 更新时间:2023-11-28 01:23:17 25 4
gpt4 key购买 nike

设计表格并尝试为有限的5个数据制作垂直滚动条。已在 css 中声明滚动,但它不起作用。试图使固定的标题表和 tbody 滚动。当更多行添加到表中时,tbody(td) 应该滚动。由于调整每一行数据,使用了 % 的宽度,并尝试了下面的代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<!-- Theme CSS -->
<link href="css/style.css" rel="stylesheet" ">
<!-- Favicon -->
<link rel="shortcut icon " href="img/favicon.ico ">
<script
src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js "></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js "></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js "></script>
<style type="text/css ">
table{
width: 800px;
position: relative;
}
thead {
display: block;
overflow-y: auto;
height: 30px;
max-width: 100%;
top: 0;
font-size: 12px;
}
tbody {
max-width: 100%;
position: absolute;
top: 40px;
height: 2em;
overflow-y:scroll;
display:block;
font-size: 10px;
}
table.scroll {
width: 100%; /* Optional */
/* border-collapse: collapse; */
border-spacing: 0;
border: 2px solid black;
}
table.scroll tbody,
table.scroll thead { display: block; }
</style>
</head>
<div class="container ">
<table class="scroll table table-bordered ">
<thead>
<tr>
<th style="width: 2.5%;font-size: 12px; ">Sl No</th>
<th style="width: 3%;font-size: 12px; ">First Name</th>
<th style="width: 10%;font-size: 12px; ">Middle Name</th>
<th style="width: 10%;font-size: 12px; ">Last Name</th>
<th style="width: 10%;font-size: 12px; ">Email</th>
<th style="width: 10%;font-size: 12px; ">Department</th>
</tr>
</thead>
<tbody>
<tr>
<td style="font-size: 11px; ">1</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">12</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">3</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">4</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">5</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">6</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">7</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">8</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">9</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

最佳答案

尝试使用下面的代码,这将在您的代码中引入垂直滚动

.scroll {
height: 100px;
overflow-y: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<!-- Theme CSS -->
<link href="css/style.css" rel="stylesheet">
<!-- Favicon -->
<link rel="shortcut icon " href="img/favicon.ico ">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js "></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js "></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js "></script>
<style type="text/css ">
table {
width: 800px;
position: relative;
}

thead {
display: block;
overflow-y: auto;
height: 30px;
max-width: 100%;
top: 0;
font-size: 12px;
}

tbody {
max-width: 100%;
position: absolute;
top: 40px;
height: 2em;
overflow-y: scroll;
display: block;
font-size: 10px;
}

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

table tbody,
table thead {
display: block;
}
</style>
</head>
<div class="container ">
<div class="scroll">
<table class=" table table-bordered ">
<thead>
<tr>
<th style="width: 2.5%;font-size: 12px; ">Sl No</th>
<th style="width: 3%;font-size: 12px; ">First Name</th>
<th style="width: 10%;font-size: 12px; ">Middle Name</th>
<th style="width: 10%;font-size: 12px; ">Last Name</th>
<th style="width: 10%;font-size: 12px; ">Email</th>
<th style="width: 10%;font-size: 12px; ">Department</th>
</tr>
</thead>
<tbody>
<tr>
<td style="font-size: 11px; ">1</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">12</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">3</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">4</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">5</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">6</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">7</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">8</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
<tr>
<td style="font-size: 11px; ">9</td>
<td style="font-size: 11px; ">User</td>
<td style="font-size: 11px; ">Name</td>
<td style="font-size: 11px;">Unknown</td>
<td style="font-size: 11px; ">username@gmail.com</td>
<td style="font-size: 11px; ">IT Development</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>

</html>

关于javascript - 垂直滚动不适用于表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51374231/

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