Skip to content

Instantly share code, notes, and snippets.

@jnf
Created October 25, 2012 16:22
Show Gist options
  • Save jnf/3953800 to your computer and use it in GitHub Desktop.
Save jnf/3953800 to your computer and use it in GitHub Desktop.
Extending ZeptoScroll (https://github.com/suprMax/ZeptoScroll) to support horizontal and vertical scrolling.
;(function($) {
var interpolate = function (source, target, shift) {
return (source + (target - source) * shift);
};
var easing = function (position) {
return (-Math.cos(position * Math.PI) / 2) + .5;
};
$.scrollBoth = function(XY, duration, easingF) {
var endX = XY[0], endY = XY[1];
duration = duration || 200;
(typeof easingF === 'function') && (easing = easingF);
var startY = window.pageYOffset, startX = window.pageXOffset,
startT = Date.now(), finishT = startT + duration;
var animate = function() {
var now = +(new Date()), shift = (now > finishT) ? 1 : (now - startT) / duration;
window.scrollTo(interpolate(startX, endX, easing(shift)), interpolate(startY, endY, easing(shift)));
(now > finishT) || setTimeout(animate, 15);
};
animate();
};
}(Zepto));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment