Skip to content

Instantly share code, notes, and snippets.

@jpescada
Created March 6, 2017 12:28
Show Gist options
  • Save jpescada/9dabc09473407e78880ab494de7b29cd to your computer and use it in GitHub Desktop.
Save jpescada/9dabc09473407e78880ab494de7b29cd to your computer and use it in GitHub Desktop.
Smooth scrolling JS
$(function() {
'use strict';
// Define only easing functions required
jQuery.extend( jQuery.easing,
{
easeOutQuart: function (x, t, b, c, d) {
return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
});
// Smooth scroll into anchors
// source: https://css-tricks.com/snippets/jquery/smooth-scrolling/
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000, 'easeOutQuart');
return false;
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment