benjamincharity
2/28/2012 - 7:52 PM

Smooth Scrolling for Local Links

Smooth Scrolling for Local Links

$('a[href*=#]').on("click", function(event) {
  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
    var $target = $(this.hash);
    $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
    if ($target.length) {
      var targetOffset = $target.offset().top;
      $('html,body').animate({scrollTop: targetOffset}, 300);
      return false;
    }
  }
});