Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created September 4, 2017 08:26
Show Gist options
  • Save ihorduchenko/36b4298d9571a11447d9c4b9bc9434cf to your computer and use it in GitHub Desktop.
Save ihorduchenko/36b4298d9571a11447d9c4b9bc9434cf to your computer and use it in GitHub Desktop.
Hide element on scrollDown, show on scrollUp
// 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