Created
January 26, 2015 01:59
-
-
Save rabidaudio/22ef920b682487f16ead to your computer and use it in GitHub Desktop.
Slide-scroll on page with jQuery
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
(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