Created
January 29, 2019 14:03
-
-
Save loughlincodes/8eb7e41d3ab01e3e0a0c3f6bcd0e3965 to your computer and use it in GitHub Desktop.
Hide Nav on Scroll Down and Show on Scroll Up
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
// Hide Header on on scroll down | |
var didScroll; | |
var lastScrollTop = 0; | |
var delta = 5; | |
var navbarHeight = $('header').outerHeight(); | |
$(window).scroll(function(event){ | |
didScroll = true; | |
}); | |
setInterval(function() { | |
if (didScroll) { | |
hasScrolled(); | |
didScroll = false; | |
} | |
}, 250); | |
function hasScrolled() { | |
var st = $(this).scrollTop(); | |
// Make sure they scroll more than delta | |
if(Math.abs(lastScrollTop - st) <= delta) | |
return; | |
// If they scrolled down and are past the navbar, add class .nav-up. | |
// This is necessary so you never see what is "behind" the navbar. | |
if (st > lastScrollTop && st > navbarHeight){ | |
// Scroll Down | |
$('header').removeClass('nav-down').addClass('nav-up'); | |
} else { | |
// Scroll Up | |
if(st + $(window).height() < $(document).height()) { | |
$('header').removeClass('nav-up').addClass('nav-down'); | |
} | |
} | |
lastScrollTop = st; | |
} |
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 { | |
padding-top: 40px; | |
} | |
header { | |
background: #f5b335; | |
height: 40px; | |
position: fixed; | |
top: 0; | |
transition: top 0.2s ease-in-out; | |
width: 100%; | |
} | |
.nav-up { | |
top: -40px; | |
} | |
main { | |
background:url( | |
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAPklEQVQYV2O8dOnSfwYg0NPTYwTRuAAj0QqxmYBNM1briFaIzRbi3UiRZ75uNgUHGbfvabgfsHqGaIXYPAMAD8wgC/DOrZ4AAAAASUVORK5CYII= | |
) repeat; | |
height: 2000px; | |
} | |
footer { background: #ddd;} | |
* { color: transparent} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment