Created
November 15, 2018 07:42
-
-
Save bobo52310/687045522118af4c2e92ce8857146a04 to your computer and use it in GitHub Desktop.
How to jump to anchor BEFORE page loads completely?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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