表格图示如下:
在尽量使用 CSS
的情况,如何实现以下需求:
1. 垂直滚动时,当前 Team 的表头部分(绿色整行),在 该Team 没有滚出视口前,保持在 Summary 的下方
2. 垂直滚动时,当前 Team 第一列的 Team Name,能否保持在 该Team 表头同一行位置,不被滚出视口
3. 横向滚动时,如何保持 Head X 表头(4列)冻结,不被滚出视口,表头列需要 宽度自适应,不能写死 `width`
4. 以上需求尽量使用 `CSS`,非必要不使用 `JS`
表格页面基础代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.frame {position:relative;width:800px;height:500px;box-shadow:0 0 0 1px #000;overflow:auto;}
.frame table {border-spacing:0;line-height:2em;}
.frame table tr th {padding:0 0.5em;font-weight:normal;white-space:nowrap;border:1px solid #CCC;}
.frame table tr td {padding:0 0.5em;border:1px solid #CCC;background-color:#FFF;}
.frame table tr.gap td {height:1px;background-color:#000;}
.frame table thead tr th {color:#369;background-color:#F0F9FF;}
.frame table tbody tr th {color:#393;background-color:#F0FFF0;}
.frame table tbody tr td {min-width:200px;color:#999;}
.frame table tfoot tr th {color:#993;background-color:#FFFFF0;}
.frame table tfoot tr td {color:#993;background-color:#FFFFF0;}
.frame table thead.sticky {position:sticky;top:0;}
.frame table tfoot.sticky {position:sticky;bottom:0;}
</style>
</head>
<body>
<div class="frame">
<table>
<thead class="sticky">
<tr>
<th rowspan="4">Summary</th>
<th>Head A</th>
<th>Head B</th>
<th>Head C</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th>Head B</th>
<th>Head C</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr class="gap">
<td colspan="7"></td>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4">Team 1</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tbody>
<tr>
<th rowspan="4">Team 2</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tbody>
<tr>
<th rowspan="4">Team 3</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tbody>
<tr>
<th rowspan="4">Team 4</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tbody>
<tr>
<th rowspan="4">Team 5</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tbody>
<tr>
<th rowspan="4">Team 6</th>
<th>Head A</th>
<th colspan="2">Head B</th>
<th>Head D</th>
<th>Head E</th>
<th>Head F</th>
<th>Head G</th>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th>Head A</th>
<th colspan="2">Head B</th>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
<tfoot class="sticky">
<tr class="gap">
<td colspan="7"></td>
</tr>
<tr>
<th colspan="4">Foot</th>
<td>f</td>
<td>f</td>
<td>f</td>
<td>f</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>