Last Updated: February 25, 2016
· leemachin

smoothly scroll to an element in javascript

Why use a massive plugin, when one line of code will cover most trivial use cases?

$('html, body').animate({'scrollTop': element.offset().top}, 'slow', 'swing');

Want it in a plugin still, so you can re-use it?

jQuery.fn.smoothScroll = function() {
  $('html, body').animate({'scrollTop': this.offset().top}, 'slow', 'swing');

Works in Chrome, Opera, Safari, IE8, and Firefox.

You can grab it as a ready made plugin here: https://github.com/leemachin/scroll

Say Thanks

1 Response
Add your response


Just for the record, I developed a library-agnostic script to do it (if you don't want be forced to use jQuery, which may be in your wish if you don't want to use something massive) : http://cl.ly/MEqo

over 1 year ago ·