thead固定和可滚动页面(不是tbody)



我有这个html代码

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="popup.css">
</head>
<body>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th title="Timezone">TZ</th>
<th>Position</th>
<th title="Time To Waypoint">TTW</th>
<th title="Distance To Waypoint">DTW</th>
<th title="Distance To Go">DTG</th>
<th title="True Wind Direction">TWD</th>
<th title="True Wind Speed">TWS</th>
<th title="True Wind Angle">TWA</th>
<th title="Bearing To Waypoint - Heading">BTW</th>
<th>Sail</th>
<th title="Speed Through Water - Boat speed">STW</th>
<th title="Average True Wind Angle">ATWA</th>
<th title="Average Bearing To Waypoint">ABTW</th>
</tr>
</thead>
<tbody id="pointsTable" align="center">
</tbody>
</table>
</div>
<br>
<div id="localtimeDiv">
<input type="checkbox" id="localtime" tabindex="-1">
<label>Local Time</label>
</div>
<div id="versionDiv">
<label>Version</label>
<label id="version"></label>
</div>
<div id="gpxDiv">
<input class="cssButton" type="button" id="gpxExport" value=".GPX" tabindex="-1">
</div>
<br>
<textarea id="gpxOutput" rows="2" readonly tabindex="-1">...:::Click on GPX button for generate file :::...
Select All | Copy selection | Paste on your text editor | Save the file with the .gpx extension</textarea>
</body>
<script src="bundle.js"></script>
</html>

和这个 CSS 代码

body {
font-size: 11px;
margin: 5px;
}
.table-wrap {
border: 1px solid black;
}
.table-wrap table {
text-align: center;
width: 100%;
}
.table-wrap table th, .table-wrap table td {
border: 1px solid black;
padding: 1px;
white-space: nowrap;
}
tr:hover td {
background-color: lightgray;
}
tr {
background-color: white;
}
#localtimeDiv {
float: left;
}
#versionDiv {
float: right;
}
#gpxDiv {
margin: 0 auto;
text-align: center;
}
.cssButton {
font-size: 11px;
}
#gpxOutput {
width: calc(100% - 5px);
resize: none;
font-size: 11px;
}

当我点击它时,我得到这个

Screen_1

我向下滚动,我得到这个

screen_2

我希望当我滚动时总是像这样可见(这是一个照片蒙太奇(

screen_3

我怎样才能把它放到位?

编辑:我无法设置固定宽度,表格永远不会相同,并且在选中"本地时间"复选框时也会演变

感谢您的帮助。

这是一个示例,它只使用一点 JS 来 CSStranslateY包装 DIV 中的THEAD

// Fix table head
function tableHeadFix(ev) {
const el = ev.target;
el.querySelector("thead").style.transform = `translateY(${el.scrollTop}px)`;
}
[...document.querySelectorAll(".table-wrap")].forEach(el =>
el.addEventListener("scroll", tableHeadFix)
);
.table-wrap{
overflow-y: auto;
height: 100px;
border: 1px solid #000;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 8px 4px;
}
th {
background: #eee;
}
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th title="Timezone">TZ</th>
<th>Position</th>
<th title="Time To Waypoint">TTW</th>
</tr>
</thead>
<tbody id="pointsTable" align="center">
<tr><td>a1</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a2</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a3</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a4</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a5</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a6</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
<tr><td>a7</td><td>b</td><td>c</td><td>d</td><td>e</td></tr>
</tbody>
</table>
</div>

如果我在 css 中替换它就可以工作

.table-wrap table th, .table-wrap table td {
border: 1px solid black;
padding: 1px;
white-space: nowrap;

}

.table-wrap table th {
position: sticky;
top: 3px;
border: 1px solid black;
background-color: white;
}
.table-wrap table td {
border: 1px solid black;
padding: 1px;
white-space: nowrap;
}

谢谢@JBDouble05,你的代码非常适合我

也感谢@Roko C. Buljan

最新更新