Reputation: 449
Hi i currently have a load of anchor links to different content on my homepage, when the user click the link it scrolls down nicely. (see code below what i am using)
Now what i would like to happen is someone can link to that content from another page lets say /#hello
currently when someone goes to /#hello (not clicking on the link) it does the default anchor state which is ok but i really need that offset in the code to happen on page loaded # link
if anyone knows a simple piece of JQuery to make this happen or can modify my code (or give me pointers) to make it happen on load too that would help me alot.
thanks
**// Scroll to
$(document).ready(function(){
$(".scroll").bind('click.smoothscroll',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top-70
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});**
Upvotes: 0
Views: 92
Reputation: 5095
on every page load you could check it like this :
if(location.hash){
$('html, body').stop().animate({
'scrollTop': $(location.hash).offset().top-120
}, 900 );
}
Upvotes: 1