Simple Animated Scrolling with jQuery

I finally added comment functionality to the blog (more on that later), and while writing the JavaScript module for comments, I wanted to have the ability to move the browser’s viewport to different parts of the page. Simply making the page jump around seemed a bit crude - thanks to jQuery, animating the scroll action is very easy.

This is just a simplified version of the much more advanced animated scrolling example over at the Learning jQuery blog. Here is my version:

scroll_to(obj) {
$("html, body").animate({
"scrollTop" : parseInt($(obj).offset().top)
}, 250);
}

Easy peasy! This function takes any search string that jQuery would understand, grabs its top offset value and makes the page scroll to it. You can see it in action in the comments form, here is how you would call it:

scroll_to("#Comments");