Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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.

#195403
Shikkediel
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.