Problem: I am trying to hide my navigation bar on scroll-down and show it on scroll-up. I placed my JavaScript to do this inside my HTML body at the bottom of the page, but it does not work. Any help is appreciated.
HTML(mainpage.component.html):
<body>
<div class="flex" id="outercontainer">
<nav id="navbar">
<a href="#" class="brand">Brand Name</a>
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#blogs">Services</a></li>
<li><a href="#gallery">Contact</a></li>
</ul>
</nav>
<div>
//content
</div>
<script type="text/javascript">
var lastScrollTop = 0;
navbar = document.getElementById("navbar");
window.addEventListener("scroll",function(){
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if(scrollTop > lastScrollTop){
navbar.style.top="-80px";
} else{
navbar.style.top="0";
}
lastScrollTop = scrollTop;
})
</script>
</body>
CSS (mainpage.component.scss)
*{
margin:0;
padding:0;
box-sizing: border-box;
font-family: 'Montserrat', sans-serif ;
scroll-behavior: smooth;
}
nav{
position: fixed;
top: 0;
width: 100%;
height: 80px;
background:#607d8b;
padding: 0 100px;
box-sizing: border-box;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.5s;
}
ul{
margin: 0;
padding: 0;
display: flex;
}
ul li{
list-style: none;
}
ul li a{
color: #fff;
padding: 0 20px;
font-size: 1.1em;
text-decoration: none;
font-weight: bold;
}
.brand{
font-size: 1.8em;
color: #fff;
font-weight: bold;
text-decoration: none;
}
body{
font-family: Arial, Helvetica, sans-serif;
background-color: #f6edd9;
text-align: center;
margin: 0;
#sidebar{
width: 10vw;
height: 100vh;
flex-direction: column;
justify-content: flex-end;
max-width: 80px;
min-width: 30px;
}
}
#banner{
width: 100%;
align-items: center;
#nameimagecontainer{
width: 60%;
}
#selfphotocontainer{
width: 30%;
}
}
#contentcontainer{
font-size: 30px;
}
#getintouchbutton{
font-style: normal;
}
#mainpagecontainer{
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
#workcontainer{
width: 100%;
height: 100vh;
background-color: green;
}
#servicescontainer{
width: 100%;
height: 100vh;
background-color: red;
}
#actioncontainer{
width: 100%;
height: 100vh;
background-color: purple;
}
#contactcontainer{
width: 100%;
height: 100vh;
background-color: orange;
}
}
So far, my script does nothing. Thank you.