Skip to content

Instantly share code, notes, and snippets.

@grantstandridge
Created March 13, 2014 14:38
Show Gist options
  • Save grantstandridge/9529703 to your computer and use it in GitHub Desktop.
Save grantstandridge/9529703 to your computer and use it in GitHub Desktop.
Sticky Nav
var stickyHeader = function($anchor,$header){
var windowTop = $(window).scrollTop(),
anchorTop = $anchor.offset().top
;
// second condition is to ensure sticky nav only initiates on desktop
if ( (windowTop > anchorTop) && (window.outerWidth > 565) ) {
$header.addClass('stuck')
;
} else {
$header.removeClass('stuck')
;
}
};
$(window).on("scroll", function(){
if (document.getElementById("stuck-nav") ) {
stickyHeader( $("#stuck-nav"), $("#stuck-nav + header") );
}
});
<div id="stuck-nav"></div>
<header>ALL VISIBLE THINGS IN HEADER</header>
header.stuck {
position: fixed;
left: auto;
top: 0;
width: 100%;
z-index: 5;
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment