Regardless of how the site quoted does, this effect is easily achieved using transition
and position: fixed
on div
of the menu.
To hide the menu, you change the top
to the value of the menu height in negative, and to show, top: 0
.
In the example below, this switch occurs when the value of scroll
is greater or less than 20 pixels.
You can set the animation time to transition: top 0.3s;
, where 0.3s
means 300 milliseconds.
var srcl_dir = 20;
window.addEventListener('scroll', function(){
var scrl = document.documentElement.scrollTop;
if(scrl >= srcl_dir){
document.querySelector("#menu").style.top = "-50px";
}else{
document.querySelector("#menu").style.top = "0";
}
srcl_dir = scrl <= 20 ? 20 : scrl;
});
html, body{
margin: 0;
padding: 0;
}
#menu{
display: block;
width: 100%;
height: 50px;
background: yellow;
position: fixed;
left: 0;
top: 0;
transition: top 0.3s;
}
#conteudo{
display: block;
height: 1200px;
padding-top: 70px;
}
<div id="menu">
menu
</div>
<div id="conteudo">
conteúdo
</div>