Forums

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

Home Forums JavaScript Scroll to anchor on interior and exterior links Re: Scroll to anchor on interior and exterior links

#139751
jefflupinski
Member

So I ended going with this…

jQuery(document).ready(function(){
jQuery(‘a[href*=#]:not([href=#])’).click(function() {
if (location.pathname.replace(/^//,”) == this.pathname.replace(/^//,”)
|| location.hostname == this.hostname) {

var target = jQuery(this.hash);
target = target.length ? target : jQuery(‘[name=’ + this.hash.slice(1) +’]’);
if (target.length) {
jQuery(‘html,body’).animate({
scrollTop: target.offset().top – 80
}, 1000);
return false;
}
}
});
});

and then for external pages traveling back to the page with the anchor links

jQuery(window).load(function(){
function goToByScroll(id){
jQuery(‘html,body’).animate({scrollTop: jQuery(“#”+id).offset().top – 80},’slow’);
}
if(window.location.hash != ”) {
goToByScroll(window.location.hash.substr(1));
}
});

now if you’re offsetting space for say a sticky nav like myself, the window is going to load at the anchor point and then animate to the desired offset upwards. This isn’t ideally what I was looking for, but this is the closest I could get.