Skip to content

Instantly share code, notes, and snippets.

@blude
Last active August 29, 2015 14:05
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 blude/9c984afc2632228e82ff to your computer and use it in GitHub Desktop.
Save blude/9c984afc2632228e82ff to your computer and use it in GitHub Desktop.
Smooth Scrollminal
// shim layer with setTimeout fallback
window.requestAnimFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
// $.scroll() function
;(function($) {
var interpolate = function (source, target, shift) {
return (source + (target - source) * shift);
};
var easing = function (pos) {
return (-Math.cos(pos * Math.PI) / 2) + 0.5;
};
$.scroll = function(endY, duration, easingF) {
endY = endY || ($.os.android ? 1 : 0);
duration = duration || 200;
(typeof easingF === 'function') && (easing = easingF);
var startY = window.pageYOffset,
startT = Date.now(),
finishT = startT + duration;
var animate = function() {
var now = +(new Date()),
shift = (now > finishT) ? 1 : (now - startT) / duration;
window.scrollTo(0, interpolate(startY, endY, easing(shift)));
(now > finishT) || requestAnimFrame(animate);
};
animate();
};
}(Zepto));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment