mul14
9/10/2016 - 11:29 PM

Smooth animated scrolling with JavaScript

Smooth animated scrolling with JavaScript

// URL: https://css-tricks.com/snippets/jquery/smooth-scrolling/#comment-1599892

$(document).on('click', 'a[href*="#"]:not([href="#"])', function(e) {

  if (location.pathname.replace(/^\//,'') === this.pathname.replace(/^\//,'') && location.hostname === this.hostname) {
    
    let target = $(this.hash);
    
    target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

    if (target.length) {

      $('html, body').animate({
        scrollTop: target.offset().top
      }, 1000);

      e.preventDefault();

    }

  }

});