Skip to content

Instantly share code, notes, and snippets.

@rogerbinns
Created August 15, 2013 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerbinns/6237680 to your computer and use it in GitHub Desktop.
Save rogerbinns/6237680 to your computer and use it in GitHub Desktop.
Fixing fixed navbar and anchors problem
(function() {
var waitTillJQuery=function() {
if(window.jQuery) {
var $=window.jQuery;
// make sure navbar is taken into account
var scrollToRightPlace=function() {
if($(".navbar-fixed-top").length) {
var navheight=$(".navbar-fixed-top").outerHeight(true);
var selector=window.location.hash;
if(!selector) return;
var offset=$(selector).offset();
// 30 is an arbitrary fudge factor so it doesn't
// end up starting exactly at the bottom of the
// navbar
window.scrollTo(offset.left, offset.top-navheight-30);
}
};
$(window).on("load hashchange", scrollToRightPlace);
// immediate run
scrollToRightPlace();
} else setTimeout(waitTillJQuery, 50);
};
waitTillJQuery();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment