Last active
August 29, 2015 14:05
-
-
Save blude/9c984afc2632228e82ff to your computer and use it in GitHub Desktop.
Smooth Scrollminal
This file contains 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
// 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