HTML:
<a href="#cat2" rel="cat2" class="scrollto">Cat 2</a> <!--Add a rel attribute-->
<a id="cat2"></a>
jQuery:
$(function () {
$('.scrollto').click(function () {
var aTag = document.getElementById(this.rel);
//I used Vanilla JS with rel tag as to get the ID correctly
$('html,body').animate({
scrollTop: aTag.offsetTop //then use Vanilla to get the offsetTop
}, 600);
//if JavaScript is turned off the link will still
//snap to the correct area
//also this method doesn't add the #cat2 to the url causing history
//issues
return false;
});
});
Test Bin http://jsbin.com/pohamesu/5
NOTE
Why use Vanilla JavaScript with jQuery? Well this will help to lower our codes weight, Vanilla JavaScript has easy methods for selectors such as document.getElementById
and the other method to get the offsets aTag.offsetTop
why use jQuery for these small methods. Only use jQuery for major stuff that may help with cross compatibility. Doc Loads, Selectors (class and elements and CSS3 selectors), also there are many of the jQuery methods that we shouldn't use if you know how to write both languages. Yes jQuery is a library though it becomes its own language as it is different than it's predecessor.