A Pen by Matthew Daniel Brown on CodePen.
Created
December 6, 2018 21:09
-
-
Save matt-daniel-brown/b31cd4a234bb5d185471a32833d84a2f to your computer and use it in GitHub Desktop.
Nav Hide on Scroll Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav> | |
<div class="inner"> | |
<span class="site-title">LOGO HERE</span> | |
</div> | |
</nav> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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