jQuery scroll to anchor

01. June 2011
I'm currently making a one-page portfolio and I wanted a nice jQuery scroll to anchor function. After some searching and testing, I stumbled on this and it works great! A lot less code than the other ones I found.

$('a[href*=#]').bind('click', function(e) {
e.preventDefault(); //prevent the "normal" behaviour which would be a "hard" jump

var target = $(this).attr("href"); //Get the target

// perform animated scrolling by getting top-position of target-element and set it as scroll target
$('html, body').stop().animate({ scrollTop: $(target).offset().top }, 400, function() {
location.hash = target; //attach the hash (#jumptarget) to the pageurl
});

return false;
});


Found it here