benjamincharity
5/9/2012 - 5:56 PM

Simple smooth scrolling solution

Simple smooth scrolling solution

// <a rel="smoothscroll" href="#foo">got to foo</a>
// <div id="foo">Foo content</div>
$(function () {
  $('a[rel=smoothscroll]').click(function( event ) {
    var location = window.location,
      hash = $(this).attr('href')
    ;
    $('html, body')
      .stop()
      .animate({scrollTop: $(hash).offset().top}, 800, function () {
        window.location = location.pathname + location.search + hash;
      })
    ;
    event.preventDefault();
  });

  var hash = window.location.hash;
  if (!!hash) {
    $('a[rel=smoothscroll][href=' + hash + ']').click();
  }
});