Skip to content

Instantly share code, notes, and snippets.

@panchr
Created January 3, 2015 18:03
Show Gist options
  • Save panchr/810336d92a7d415eac5f to your computer and use it in GitHub Desktop.
Save panchr/810336d92a7d415eac5f to your computer and use it in GitHub Desktop.
A small jQuery-driven script to smooth-scroll to anchors
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