Skip to content

Instantly share code, notes, and snippets.

@artygrand
Created December 9, 2016 11:09
Show Gist options
  • Save artygrand/8f0ea2570b20c7d80dace2bc73dfcb92 to your computer and use it in GitHub Desktop.
Save artygrand/8f0ea2570b20c7d80dace2bc73dfcb92 to your computer and use it in GitHub Desktop.
Fixed nav on scroll
var nav_offset = $('nav').offset().top,
nav_height = $('nav').outerHeight(true);
$('nav').after($('<div>').addClass('padding').css('height', nav_height));
$(window).scroll(function(){
if ($(this).scrollTop() > nav_offset){
$('nav').addClass('fixed')
} else {
$('nav').removeClass('fixed')
}
});
nav.fixed {position: fixed;top: 0;width: 100%;z-index: 1000;}
nav+.padding {display: none;}
nav.fixed+.padding {display: block;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment