Skip to content

Instantly share code, notes, and snippets.

@rabidaudio
Created January 26, 2015 01:59
Show Gist options
  • Save rabidaudio/22ef920b682487f16ead to your computer and use it in GitHub Desktop.
Save rabidaudio/22ef920b682487f16ead to your computer and use it in GitHub Desktop.
Slide-scroll on page with jQuery
(function($){
$.fn.scrollTo = function(maxTime, callback){
if(typeof maxTime === "function"){
callback = maxTime;
maxTime = 2000;
}else if(!maxTime){
maxTime = 2000;
}
if(!callback){
callback = new Function();
}
var dest_pos = $(this).offset().top;
var distance = dest_pos - $(document).scrollTop();//down is positive
var done = false;
$('body,html').animate({ scrollTop: dest_pos }, Math.min(maxTime, Math.abs(distance)), function(){
if(!done){
done = true;
callback.call(this);
}
});
};
})(jQuery);
$(document).ready(function(){
$('a[href^="#"]').click(function(e){
e.preventDefault();
var dest = $(this).attr('href');
$(dest).scrollTo(700, function(){
window.location.hash = dest.substr(1);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment