Skip to content

Instantly share code, notes, and snippets.

@bobo52310
Created November 15, 2018 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobo52310/687045522118af4c2e92ce8857146a04 to your computer and use it in GitHub Desktop.
Save bobo52310/687045522118af4c2e92ce8857146a04 to your computer and use it in GitHub Desktop.
How to jump to anchor BEFORE page loads completely?
// ref: https://stackoverflow.com/questions/43232931/how-to-jump-to-anchor-before-page-loads-completely
function autoScroll(name, animated, distance){
var scrollDistance = distance;
var scrollTarget = $("*[name='"+name+"']");
if(animated){
$('html, body').animate({scrollTop: (scrollTarget.offset().top - scrollDistance)}, 1300);
}
else{
$('html, body').scrollTop(scrollTarget.offset().top - scrollDistance);
}
}
//Stop default hashtag scrolling
window.scrollTo(0, 0);
//Execute stuff when entire page is loaded
$(window).bind("load", function() {
//Check if there is a hashtag in URL
if(window.location.hash) {
//Get hashtag in URL
var getHash = window.location.hash.substring(1);
//Do the scroll function to the desired section
autoScroll(getHash,true,0);
}
//Do it manually
autoScroll("backtolist",true,0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment