July 8, 2012
Scrolling to a particular div
Question by user1510326
I have an html page with three divs containing text content. I want to scroll to a particular div depending upon the link that the user clicks on the home page.
There are three links on the home page; so what I want is if the user clicks on link 1 he is scrolled to div 1 on the resultant page, if he clicks on link 2 on the home page, then the page is scrolled to div 2 on resultant page and so on.
Answer by Starx
Easiest method is using Fragment Identifier. Whenever you are creating links, attach the id of the element you want to scroll to, on the end of link. For example:
link.html#divToScrollTo
An example usage:
<a href="link.html#divToScrollTo">Scroll to div with divToScrollTo as id</a>
After clicking on this link, the browser to first navigate to link.html
and then scroll to an element with divToScrollTo
Link.