Last active
August 29, 2015 14:01
-
-
Save Takaya213/8952cbfe6133aadece82 to your computer and use it in GitHub Desktop.
Scroll Up Bar
This file contains 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 { | |
height: 2000px; | |
} | |
footer { background: #ddd;} |
This file contains 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
<header class="nav-down"> | |
This is your menu. | |
</header> | |
<main> | |
This is your body. | |
</main> | |
<footer> | |
This is your footer. | |
</footer> |
This file contains 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment