Created
September 10, 2016 23:29
-
-
Save mul14/bddd1fcfd9712f47fc765c4b52be6adf to your computer and use it in GitHub Desktop.
Smooth animated scrolling with JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment