gpt4 book ai didi

Javascript 表头不粘

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:15 26 4
gpt4 key购买 nike

<分区>

我尝试根据在 W3Schools 上找到的解决方案实现一个粘性表头: https://www.w3schools.com/howto/howto_js_sticky_header.asp

但是我的表格标题仍然没有响应滚动通过标题。控制台日志给出了 stickywindow.pageYOffsetheader.offsetHeight 的预期值。我错过了什么吗?

我的代码总结如下:

<html lang="en">
<head>
<style type="text/css">
/*...*/
#myTable {
float:left;
width:75%;
background-color:#fff;
padding:6px;
margin-left:175px;
}

#myTable th {
cursor:pointer;
position: sticky;
top: 0;
}

#myTable th:hover {
background-color:#66991c;
color:white;
}

#myTable tr:nth-child(even) {
background-color:#eee;
}

#myTable tr:nth-child(odd) {
background-color:#fff;
}

#table-link {
display:block;
text-decoration:none;
background-color:black;
color:white;
border-radius:5px;
}

#table-link a {
display:block;
text-decoration:none;
background-color:black;
color:white;
border-radius:5px;
}

#table-link a:hover {
display:block;
text-decoration:none;
background-color:#66991c;
color:white;
border-radius:5px;
}

.sticky {
position: fixed;
top: 0;
width: 100%;
}
/*...*/
</style>
</head>
<body>
...
<table id="myTable">
<div id="myHeader">
<caption>...</caption>
<thead>...</thead>
</div>
<tbody>...</tbody>
</table>
...

<script>
window.onscroll = function() {myFunction()};

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
if (window.pageYOffset > sticky) {
header.style.paddingTop = header.offsetHeight + 'px';
header.classList.add("sticky");
} else {
header.style.paddingTop = 0;
header.classList.remove("sticky");
}
}
</script>
</body>
</html>

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