Created
January 3, 2015 18:03
-
-
Save panchr/810336d92a7d415eac5f to your computer and use it in GitHub Desktop.
A small jQuery-driven script to smooth-scroll to anchors
This file contains 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
jQuery.fn.smoothScroll = function(options) { | |
var options = jQuery.extend({offset: 0, duration: 900, easing: "swing"}, options); | |
$('html, body').stop().animate({'scrollTop': this.offset().top + options.offset}, options.duration, options.easing); | |
} | |
function anchorLinkSmoothScroll() { | |
// Smooth scroll to any clicked anchor link | |
$("a[href^=#]").click(function (event) { | |
event.preventDefault(); | |
var anchorLink = this.href.replace(this.baseURI, ""); | |
$(anchorLink).smoothScroll(); | |
$("html, body").promise().done(function() {window.history.replaceState(null, "Change Current Anchored Section", anchorLink)}); | |
return false; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment