Created
September 4, 2017 08:26
-
-
Save ihorduchenko/36b4298d9571a11447d9c4b9bc9434cf to your computer and use it in GitHub Desktop.
Hide element on scrollDown, show on scrollUp
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, | |
lastScrollTop = 0, | |
delta = 5, | |
navbarHeight = $('.navbar-blue_grid').outerHeight(), | |
socials = $('#fixed-social'); | |
$(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 | |
socials.addClass('nav-up'); | |
} else { | |
// Scroll Up | |
if(st + $(window).height() < $(document).height()) { | |
socials.removeClass('nav-up'); | |
} | |
} | |
lastScrollTop = st; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment