Reputation: 29
What I am trying to achieve is that if someone clicks on the specified link in the menu, it redirects to the homepage and then scrolls to the specific content.
My Jquery code is this simple:
$(document).ready(function () {
$("#upbar li:nth-of-type(2)").click(function(){
$(location).attr('href', 'index.html');
$('html,body').animate({
scrollTop: $("#samples").offset().top}, 700);
});
});
It redirects to the index.html page but it does not scroll :/
Thank you in advance!
Martin
Upvotes: 0
Views: 56
Reputation: 93571
Assuming location
is targetting window.location
in your example to change browser URL...
Changing the location
of the browser will load a new page and throw away the running jQuery/JavaScript. $('html,body').animate(
will never run on the intended new page.
e.g.
$(location).attr('href', 'index.html#samples');
or better yet just:
location.href = "index.html#samples";
Or you can load the page using Ajax, so your code stays resident
Or pass something to the new page to tell it where to scroll
Upvotes: 2