@synapsestudios/react-scroll
Version:
A scroll component for React.js
91 lines (67 loc) • 2.15 kB
JavaScript
var smooth = require('./smooth');
var easing = smooth.defaultEasing;
var cancelEvents = require('./cancel-events');
/*
* Sets the cancel trigger
*/
cancelEvents.register(function() {
__cancel = true;
});
/*
* Wraps window properties to allow server side rendering
*/
var currentWindowProperties = function() {
if (typeof window !== 'undefined') {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame;
}
};
/*
* Helper function to never extend 60fps on the webpage.
*/
var requestAnimationFrame = (function () {
return currentWindowProperties() ||
function (callback, element, delay) {
window.setTimeout(callback, delay || (1000/60));
};
})();
var __currentPositionY = 0;
var __startPositionY = 0;
var __targetPositionY = 0;
var __progress = 0;
var __duration = 0;
var __cancel = false;
var __start;
var __deltaTop;
var __percent;
var currentPositionY = function() {
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
return supportPageOffset ? window.pageYOffset : isCSS1Compat ?
document.documentElement.scrollTop : document.body.scrollTop;
};
var animateTopScroll = function(timestamp) {
// Cancel on specific events
if(__cancel) { return };
__deltaTop = Math.round(__targetPositionY - __startPositionY);
if (__start === null) {
__start = timestamp;
}
__progress = timestamp - __start;
__percent = (__progress >= __duration ? 1 : easing(__progress/__duration));
__currentPositionY = __startPositionY + Math.ceil(__deltaTop * __percent);
window.scrollTo(0, __currentPositionY);
if(__percent < 1) {
requestAnimationFrame(animateTopScroll);
}
};
var startAnimateTopScroll = function(y, options) {
__start = null;
__cancel = false;
__startPositionY = currentPositionY();
__targetPositionY = y + __startPositionY;
__duration = options.duration || 1000;
requestAnimationFrame(animateTopScroll);
};
module.exports = {
animateTopScroll: startAnimateTopScroll
};