Home › Forums › JavaScript › smooth anchor link scroll, anchor point sticky header compensation, etc. › Reply To: smooth anchor link scroll, anchor point sticky header compensation, etc.
February 9, 2015 at 2:07 pm
#195403
Participant
No worries, if the header’s height is fixed you could apply it directly :
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top-68 // right here
}, 1000);
return false;
}
}
});
});
What I had not accounted for is that the height may be set after the above function runs (so trying to get it’s height dynamically wouldn’t work yet) but accounting for it directly should trigger in any case.