Javascript 我试图让头球崩溃
当它向下滚动时,我试图更改标题 html: js: 由于某种原因,它不起作用。有人能帮忙吗Javascript 我试图让头球崩溃,javascript,html,css,Javascript,Html,Css,当它向下滚动时,我试图更改标题 html: js: 由于某种原因,它不起作用。有人能帮忙吗 var header=document.getElementById('header'); var sticky=header.offsetTop; 函数myFunction(){ 如果(window.pageYOffset>sticky){ header.classList.add('sticky'); }否则{ header.classList.remove('sticky'); } } windo
var header=document.getElementById('header');
var sticky=header.offsetTop;
函数myFunction(){
如果(window.pageYOffset>sticky){
header.classList.add('sticky');
}否则{
header.classList.remove('sticky');
}
}
window.addEventListener('scroll',myFunction)代码>
正文{
背景:红色;
高度:800px;
}
.头{
宽度:100%;
高度:70像素;
背景色:黑色;
}
.粘的{
背景:黄色;
位置:固定!重要;
高度:40px;
排名:0;
宽度:100%;
}
可能尝试使用正确的id:var header=document.getElementById(“header”)代码>?是的nvm它工作了id是正确的我的浏览器出了问题,所以我不得不重新启动它真的失去了理智,甚至为此建立了一个堆栈溢出帐户
<div id="header" class="head"> </div>
.head {
width:100%;
height: 70px;
background-color: black;
}
.sticky {
position: fixed !important;
height: 40px;
top: 0;
width: 100%;
}
window.onscroll = function() {myFunction()};
var header = document.getElementById("myHeader");
var sticky = header.offsetTop;
function myFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
}
else {
header.classList.remove("sticky");
}
}