I am attempting to automatically scroll to the div specified in the URL. The URL may include one of 21 different IDs such as:
url.com/test#lite1
url.com/test#lite2
url.com/test#lite3
This scrolling action should happen when the page loads (so that users coming from an ebook can see the exact item they clicked on).
Below is the code I am currently using:
$(document).ready(function(){
$('a[href^="#"]').load(function() {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top -150
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
</script>
The code is not functioning correctly, and I suspect it's due to this section (which I'm unsure about):
$('a[href^="#"]').load(function() {
Additionally, I want the scrolled-to div to be positioned in the center of the page (rather than at the top where browsers typically place it). Can you confirm if this adjustment is correct?:
$target.offset().top -150
Thank you for your assistance!