Regardless of how the cited site does, this effect is easily achieved using transition
and position: fixed
in div
menu.
To hide the menu, you change the top
for the menu height value in negative, and to show, top: 0
.
In the example below, this alternation occurs when the value of scroll
is greater than or less than 20 pixels.
You can adjust the animation time in 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>
On the contrary, when it rolls down, the menu disappears, then it rolls up and it appears. It wouldn’t be that?
– Sam
No ...when the page scrolls down the fixed menu at the top. When the menu scrolls up it goes away. But regardless of this. How is this action happening? did not find the js or css that is running this operation
– Israel Zebulon
With me as with the DVD, rolled down disappeared, but a small scroll up the menu appeared, regardless of the position of the page.
– Guilherme Nascimento