Skip to content

Instantly share code, notes, and snippets.

@matt-daniel-brown
Created December 6, 2018 21:09
Show Gist options
  • Save matt-daniel-brown/b31cd4a234bb5d185471a32833d84a2f to your computer and use it in GitHub Desktop.
Save matt-daniel-brown/b31cd4a234bb5d185471a32833d84a2f to your computer and use it in GitHub Desktop.
Nav Hide on Scroll Down
<nav>
<div class="inner">
<span class="site-title">LOGO HERE</span>
</div>
</nav>
var scrollSet = null;
var didScroll = false;
var windowTop = $(window).scrollTop();
function scrollUpdate () {
didScroll = true;
windowTop = $(window).scrollTop();
}
function scrollTicker () {
if(didScroll) {
if(windowTop > scrollSet) {
$('body').addClass('hide-nav')
scrollSet = windowTop;
}
else if(scrollSet > windowTop){
$('body').removeClass('hide-nav')
scrollSet = windowTop;
}
didScroll = false
}
requestAnimationFrame(scrollTicker);
}
$(window).scroll(scrollUpdate);
scrollTicker();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
body{
height: 3000px;
font-family: helvetica;
margin: 0;
}
nav{
background: #eee;
width: 100%;
position: fixed;
transition: all 400ms ease;
top: 0;
transform: translateY(0%);
.inner{
padding: 2em 0em;
}
}
.hide-nav{
nav{
transform: translateY(-100%);
}
}
.inner{
max-width: 1000px;
margin: auto;
}
.site-title{
font-weight: 800;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment