I have this Twitter Bootstrap code
-
Two problems will happen here:
- Page load (content hidden)
- Internal links like this will scroll to the top, and be hidden by the navbar:
hello
World
Bootstrap 4 w/ internal page links
To fix 1), as Martijn Burger said above, the bootstrap v4 starter template css uses:
body {
padding-top: 5rem;
}
To fix 2) check out this issue. This code mostly works (but not on 2nd click of same hash):
window.addEventListener("hashchange", function() { scrollBy(0, -70) })
This code animates A links with jQuery (not slim jQuery):
// inline theme global code here
$(document).ready(function() {
var body = $('html,body'), NAVBAR_HEIGHT = 70;
function smoothScrollingTo(target) {
if($(target)) body.animate({scrollTop:$(target).offset().top - NAVBAR_HEIGHT}, 500);
}
$('a[href*=\\#]').on('click', function(event){
event.preventDefault();
smoothScrollingTo(this.hash);
});
$(document).ready(function(){
smoothScrollingTo(location.hash);
});
})