Skip to content

Instantly share code, notes, and snippets.

@chrisjreber
Last active December 27, 2017 15:24
Show Gist options
  • Save chrisjreber/980d87c89ec57ee0c4a0422271cf1546 to your computer and use it in GitHub Desktop.
Save chrisjreber/980d87c89ec57ee0c4a0422271cf1546 to your computer and use it in GitHub Desktop.
sticky header nav. hide on scrolldown past point X and appear on scrollup past X scrollup distance
<script>
$( document ).ready(function() {
var distance = 0;
if ($(".toggle-menu").css('display') == 'none') {
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('#site-header').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled(this);
didScroll = false;
}
}, 250);
function hasScrolled(item) {
var st = $(item).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
$('#site-header').removeClass('nav-down').addClass('nav-up');
$('.megamenu__wrap').stop().hide();
$('.product__nav').removeClass('push-down');
$('.desktop-menu li').removeClass('active');
$('.desktop-menu li').children('ul').stop().slideUp();
distance = 0;
} else {
// Scroll Up
if (st + $(window).height() < $(document).height()) {
// Scroll up distance to prevent accidental slide in
distance++;
if (distance >= 5) {
$('#site-header').removeClass('nav-up').addClass('nav-down');
$('.product__nav').addClass('push-down');
}
}
// If user is at top of page
if (st < 100) {
$('#site-header').removeClass('nav-up').addClass('nav-down');
$('.product__nav').addClass('push-down');
}
}
lastScrollTop = st;
}
}// end if .toggle-menu
});// end document.ready
</script>
<style>
.admin-bar #site-header {
top: 32px;
}
.fixed {
position: fixed !important;
}
.fixed.push-down {
top: 165px;
border-top: 4px solid $white;
}
// .admin-bar .product__nav {
// top: 32px;
// }
.admin-bar .fixed.push-down {
top: 197px;
}
@include breakpoint(large) {
#site-header {
height: 165px;
}
#site-header.nav-up {
top: -165px;
}
body {
padding-top: 165px;
}
}
@include breakpoint(small only) {
#site-header {
height: 105px;
position: relative;
}
#site-header.nav-up {
top: -105px;
}
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment