Skip to content

Instantly share code, notes, and snippets.

@mrofi
Created December 14, 2014 17:47
Show Gist options
  • Select an option

  • Save mrofi/5146a8e0a2a76fb95221 to your computer and use it in GitHub Desktop.

Select an option

Save mrofi/5146a8e0a2a76fb95221 to your computer and use it in GitHub Desktop.
This function can make navbar become fixed-top navbar when user do scrolling and top of the navbar meets the top border of browser.
jQuery(function() {
window.magellan = function() {
obj = $('.magellan');
pos = obj.offset().top;
pos2 = obj.next().offset().top-obj.innerHeight();
$(window).scroll(function() {
if (pos < $(this).scrollTop() && !obj.hasClass('navbar-fixed-top')) return obj.addClass('navbar-fixed-top');
if (pos2 > $(this).scrollTop()) return obj.removeClass('navbar-fixed-top');
});
}
})
// Run with call : window.magellan() and don't forget to add class "magellan" into navbar that you want to work with.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment