New to JavaScript. I'm trying to get a header element that disappears when scolling down, and appears when scrolling up.
Why isn't this selecting the <header>
element?
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementsByTagName("header").style.top = "0";
} else {
document.getElementsByTagName("header").style.top = "-72px";
}
prevScrollpos = currentScrollPos;
}
header {
height: 72px;
background-color: red;
position: fixed;
top: 0;
left: 0;
right: 0;
transition: top 0.2s ease-in-out;
z-index: 200;
}
But if I add an ID to the <header>
I can select it using the following
document.getElementById("navbar")