Skip to content

Instantly share code, notes, and snippets.

@lagden
Created September 2, 2014 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagden/d845ec27162a370ab937 to your computer and use it in GitHub Desktop.
Save lagden/d845ec27162a370ab937 to your computer and use it in GitHub Desktop.
scrollToAnimated
// scrollToAnimated
(function(window) {
'use strict';
// provides requestAnimationFrame in a cross browser way
window.requestAnimFrame = (function() {
'use strict';
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element) {
return window.setTimeout(callback, 1000 / 60);
};
})();
// ease
Math.easeInOutQuad = function(t, b, c, d) {
t /= d / 2;
if (t < 1) {
return c / 2 * t * t + b;
}
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
};
// scrollTo
function scrollToAnimated(to, callback, duration) {
var doc = document.body,
start = doc.scrollTop,
change = to - start,
currentTime = 0,
increment = 20,
raf;
duration = (typeof(duration) === 'undefined') ? 500 : duration;
function animateScroll() {
currentTime += increment;
var val = Math.easeInOutQuad(currentTime, start, change, duration);
doc.scrollTop = val;
if (currentTime < duration) {
raf = requestAnimFrame(animateScroll);
} else {
cancelAnimationFrame(raf);
if (callback && typeof(callback) === 'function') {
callback();
}
}
}
animateScroll();
}
// AMD Support
if (typeof define === 'function' && define.amd)
define(function() {
return scrollToAnimated;
});
else
window.scrollToAnimated = scrollToAnimated;
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment