我有这个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;
}
当我点击它时,我得到这个
我向下滚动,我明白了
我希望当我滚动时总是这样可见(这是照片蒙太奇)
我该如何放置它?
编辑:我无法设置固定宽度,表格永远不会相同,并且在选中“本地时间”复选框时也会发生变化
感谢您的帮助。
答案 0 :(得分:0)
下面是一个示例,该示例在包装的DIV中仅使用少量JS来CSS translateY
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>
答案 1 :(得分:0)
如果我将其替换为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