gpt4 book ai didi

javascript - 我正在尝试使页眉折叠

转载 作者:行者123 更新时间:2023-11-28 02:25:07 25 4
gpt4 key购买 nike

我正在尝试在向下滚动时更改标题

html:

<div id="header" class="head"> </div>

CSS:

.head {
width:100%;
height: 70px;
background-color: black;
}
.sticky {
position: fixed !important;
height: 40px;
top: 0;
width: 100%;
}

js:

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");
}
}

由于某种原因它不起作用任何人都可以帮忙!?!?

最佳答案

var header = document.getElementById('header');
var sticky = header.offsetTop;

function myFunction() {
if (window.pageYOffset > sticky) {
header.classList.add('sticky');
} else {
header.classList.remove('sticky');
}
}

window.addEventListener('scroll', myFunction);
body {
background: red;
height: 800px;
}

.head {
width: 100%;
height: 70px;
background-color: black;
}

.sticky {
background: yellow;
position: fixed !important;
height: 40px;
top: 0;
width: 100%;
}
<div id="header" class="head"></div>

关于javascript - 我正在尝试使页眉折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54710917/

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