web development
Posted by: Nico
Categories: HTML|Javascript
1 Apr 2010Question :
How to use javascript to jump or scroll to an anchor or specific element in your HTML page?
I know some of you were thinking, why not just use the anchor hash trick?
Like this:
<a href="#jump_location_id">Jump to the on-page location</a>
.... random lines of code ...
.... random lines of code ...
.... random lines of code ...
.... random lines of code ...
<div id="jump_location_id">
Supposed to jump here
</div>
While the anchor hash trick is cool, quick and easy, there is one con — it gives you an ugly url containing hashes and id references. So if that matters in your website, here is a javascript solution to jump around on a page while nothing happens to your url:
Answer :
First the HTML :
<a href="jump_location_id" id="jump_to_loc">Jump to the on-page location</a>
.... random lines of code ...
.... random lines of code ...
.... random lines of code ...
.... random lines of code ...
<div id="jump_location_id">
Supposed to jump here
</div>
Then add the following javascript code, which also uses jquery :
$('#jump_to_loc').click(function(){
var jump = $(this).attr('href');
var new_position = $('#'+jump).offset();
window.scrollTo(new_position.left,new_position.top);
return false;
});
The key to the effect is the javascript window.scrollTo(x,y) function.
Note: there is also a Jquery plugin that can do this, but I have not tried it yet — I refrain from using too many plugins as I lose track of everything I include and don’t like to clutter my server. The plugin is called ScrollTo.
Based in South Africa, we're a web-development company...
(2)
(3)
(1)
(5)
(4)
(1)
(1)
(14)
(1)
(8)
(7)
(17)
(1)
(4)
(2)
(2)
(1)
11 Responses to javascript jump to anchor or element using jquery
mAu
May 23rd, 2011 at 5:01 pm
There is an easier way: window.location.hash = ‘#jump_to_loc’;
Nico
May 24th, 2011 at 9:19 am
Hi mAu. I think you might have misread the post a bit. The whole idea is to jump to a location on the page WITHOUT showing hashtags in the url. window.location.hash adds the hashtag in the url and therefore is not the clean solution we are looking for. Thanks for the comment
azazello
September 3rd, 2011 at 8:27 pm
Thx, guys!!! Wery good post!
nightmd
October 11th, 2011 at 3:58 pm
I have implemented this exactly and keep ketting a “page not found” error. I am hosting it locally. Is this something that only works when it is posted to the server? Am I putting the script in the wrong place? A finished html document to view might be quite helpful. Thanks.
Nico
October 11th, 2011 at 4:50 pm
Hi nightmd
If you get a “page not found” then it means the page is trying to reload or load a new page. Our script does not let the page reload but merely scrolls on it. This might be because your jscript or jquery is not active or is not compiling correctly
bird
November 24th, 2011 at 9:36 am
nice work …really help me out !many thanks
Benji
December 9th, 2011 at 7:17 pm
Pefect! Using it for form validation, user is forced to look at what they did wrong now! Love it!
Veli
December 22nd, 2011 at 11:14 am
neat and elegant! thanks :)
Stefan
December 23rd, 2011 at 12:08 pm
Is there a way to use the jquery anchor where the link directs to another page and calls the anchor?
Nico
January 3rd, 2012 at 8:24 am
@Stefan – your javascript loads from scratch on every pageload, so if you wish to do that on a new page you will have to send the anchor id through some other way… perhaps in the url (www.website.com/anchor_id). Maybe you can try posting the information to the next page, then catch it with your serverside script (PHP, ASP), and set a variable in the javascript.
martin
April 18th, 2012 at 11:12 pm
just what i was looking for, thanks!