Forked from iamravenous/jquery.smooth-scroll-anchor-link.js
Last active
September 4, 2018 05:14
-
-
Save soham2008xyz/83370d89b02a8f03d4a5004236ef08fd to your computer and use it in GitHub Desktop.
Automatically detects the hash and scroll smoothly to anchor link with URL hashchange
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
/* | |
* Smooth scroll to anchor link | |
* Automatically detects the hash and scroll smoothly to anchor link with URL hashchange | |
* Author: Soham Banerjee @soham2008xyz | |
*/ | |
// If you need more autonomy, | |
// You can replace hash detection with a data-attribute | |
// e.g. $("[data-scroll='smooth']") | |
$(document).on("click", "a[href*='#']:not([href='#'])", function (e) { | |
e.preventDefault(); | |
var hash = this.hash; | |
var section = $(hash); | |
if (section) { | |
$('html, body').animate({ | |
scrollTop: section.offset().top | |
}, 1000, 'swing', function () { | |
history.replaceState({}, "", hash); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment