Skip to content

Instantly share code, notes, and snippets.

@clayb
Last active April 28, 2016 21:10
Show Gist options
  • Save clayb/825102e185912bd3ebfc to your computer and use it in GitHub Desktop.
Save clayb/825102e185912bd3ebfc to your computer and use it in GitHub Desktop.
Navigation dropdown on page scroll
openDropdownNav = function() {
return $('header').addClass('dropdown-nav').slideDown();
};
closeDropdownNav = function() {
return $('header.dropdown-nav').slideUp(function() {
return $('header').removeClass('dropdown-nav').stop().removeAttr('style');
});
};
getDocHeight = function() {
var D;
D = document;
return Math.max(D.body.scrollHeight, D.documentElement.scrollHeight, D.body.offsetHeight, D.documentElement.offsetHeight, D.body.clientHeight, D.documentElement.clientHeight);
};
lastScroll = 0;
$(window).bind('scroll', function() {
var bottom, currentScroll, pastFirstFrame, top;
currentScroll = $(this).scrollTop();
pastFirstFrame = currentScroll > $('#front, #about-header, body.services #sanky-divisions, #work-header, .work-sample-header, .blog-header').height();
top = currentScroll <= 150;
bottom = currentScroll + window.innerHeight >= getDocHeight() - 100;
if (bottom) {
openDropdownNav();
}
if (top && $('header.dropdown-nav').is(':visible')) {
closeDropdownNav();
}
if (!bottom) {
if (currentScroll > lastScroll && $('header.dropdown-nav').is(':visible')) {
closeDropdownNav();
}
}
if (currentScroll < lastScroll && pastFirstFrame) {
openDropdownNav();
}
return lastScroll = currentScroll;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment