I was trying to use jQuery's page scroll inside some pages and could successfully make a smooth page scroll. The only problem I have now is when attempting to do that from different page. What I mean by that is if I click on a link in a page, it should load the new page and then scroll to the specific div element.
Here is the code I used to scrolling inside the page:
var jump=function(e)
{
//prevent the "normal" behaviour which would be a "hard" jump
e.preventDefault();
//Get the target
var target = $(this).attr("href");
//perform animated scrolling
$('html,body').animate(
{
//get top-position of target-element and set it as scroll target
scrollTop: $(target).offset().top
//scrolldelay: 2 seconds
},2000,function()
{
//attach the hash (#jumptarget) to the pageurl
location.hash = target;
});
}
$(document).ready(function()
{
$('a[href*=#]').bind("click", jump);
return false;
});
I hope the idea is clear.
Thanks
Very important Note: This code I posted above works great inside the same page, but what I'm after is to click a link from one page and go to another one and then scroll to the target. I hope it is clear now. Thanks
On the link put a hash:
And on other page, you can do:
On other page, you should have element with id set to
elementID
to scroll to. Of course you can change the name of it.here "bottom" is the div tag id where you want to scroll to. For changing the animation effects, you can change the time from '1' to a different value
I would like to recommend using the scrollTo plugin
http://demos.flesler.com/jquery/scrollTo/
You can the set scrollto by jquery css selector.
I have had great luck with the accuracy of this plugin and its methods, where other methods of achieving the same effect like using
.offset()
or.position()
have failed to be cross browser for me in the past. Not saying you can't use such methods, I'm sure there is a way to do it cross browser, I've just found scrollTo to be more reliable.Combining answers by Petr and Sarfraz, I arrive at the following.
On page1.html:
On page2.html:
I made a reusable plugin that can do this... I left the binding to events outside the plugin itself because I feel it is too intrusive for such a little helper....
Now next, we can onload just do this, check for a hash and if its there try to use it directly as a selector for jQuery. Now I couldn't easily test this at the time but I made similar stuff for production sites not long ago, if this doesn't immediatly work let me know and I'll look into the solution I got there.
(script should be within an onload section)
Next we bind the plugin to onclick of anchors which only contain a hash in their href attribute.
(script should be within an onload section)
Since jQuery doesn't do anything if the match itself fails we have a nice fallback for when a target on a page can't be found yay \o/
Update
Alternative onclick handler to scroll to the top when theres only a hash:
Here is also a simple jsfiddle that demonstrates the scrolling within page, onload is a little hard to set up...
http://jsfiddle.net/sg3s/bZnWN/
Update 2
So you might get in trouble with the window already scrolling to the element onload. This fixes that:
window.scrollTo(0,0);
it just scrolls the page to the left top. Added it to the code snippet above.You basically need to do this:
href="other_page.html#section"
)ready
handler clear the hard jump scroll normally dictated by the hash and as soon as possible scroll the page back to the top and calljump()
- you'll need to do this asynchronouslyjump()
if no event is given, makelocation.hash
the targethtml,body
right away and show it back once you scrolled it back to zeroThis is your code with the above added:
Verified working in Chrome/Safari, Firefox and Opera. Dunno about IE though.