Created
August 15, 2013 02:23
-
-
Save rogerbinns/6237680 to your computer and use it in GitHub Desktop.
Fixing fixed navbar and anchors problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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