Skip to content

Instantly share code, notes, and snippets.

@baniol
Created March 30, 2013 16:51
Show Gist options
  • Save baniol/5277417 to your computer and use it in GitHub Desktop.
Save baniol/5277417 to your computer and use it in GitHub Desktop.
sticky navbar for bootstrap from twitter
// sticky navbar
$(document).scroll(function(){
// If has not activated (has no attribute "data-top"
if (!$('.subnav').attr('data-top')) {
// If already fixed, then do nothing
if ($('.subnav').hasClass('subnav-fixed-top')) return;
// Remember top position
var offset = $('.subnav').offset()
$('.subnav').attr('data-top', offset.top);
}
if ($('.subnav').attr('data-top') - $('.subnav').outerHeight() <= $(this).scrollTop())
$('.subnav').addClass('subnav-fixed-top');
else
$('.subnav').removeClass('subnav-fixed-top');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment