jsBin demo
<ul id="links"> <li><a href="#a">Go to a</a></li> <li><a href="#b">Go to b</a></li> </ul>
and than anywhere else in your document ...
<h2 id="a">Article "a"</h2> Lorem...... <h2 id="b">Article "b"</h2> Lorem......
JQ:
$('#links a').click(function( e ){ e.preventDefault(); var targetId = $(this).attr("href"); var top = $(targetId).offset().top; $('html, body').stop().animate({scrollTop: top }, 1500); });
what is stated above is to use the extracted href anchor and use it as a jQuery # (id) selector. This ID element is found, return it up offset and finally leave the page.
Roko C. Buljan
source share