Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Singleton
- * @param {Object} [options]
- * @param {String} [options.axis] --- x | y | xy
- * @param {Number} [options.acceleration]
- * @param {Function} [options.callback]
- * @return {Object}
- */
- function scrollInitial(options) {
- options = Object(options);
- options.axis = options.axis || "y";
- options.acceleration = Math.abs(options.acceleration) || 0.1;
- var requestID;
- var speed = 1 + options.acceleration;
- var defaultView = document.defaultView;
- var ix = options.axis === "x" || options.axis === "xy";
- var iy = options.axis === "y" || options.axis === "xy";
- if (!ix && !iy) {
- throw new TypeError("Unexpected token");
- }
- var requestScrollFrame = function () {
- var x1 = 0, y1 = 0;
- var x2 = 0, y2 = 0;
- var x3 = 0, y3 = 0;
- var x, y, xMax, yMax, inProgress;
- if (document.body) {
- x1 = document.body.scrollLeft || 0;
- y1 = document.body.scrollTop || 0;
- }
- if (document.documentElement) {
- x2 = document.documentElement.scrollLeft || 0;
- y2 = document.documentElement.scrollTop || 0;
- }
- x3 = defaultView.scrollX || 0;
- y3 = defaultView.scrollY || 0;
- xMax = Math.max(x1, x2, x3);
- yMax = Math.max(y1, y2, y3);
- if (ix && iy) {
- inProgress = xMax > 0 || yMax > 0;
- } else if (ix) {
- inProgress = xMax > 0;
- } else if (iy) {
- inProgress = yMax > 0;
- }
- x = ix ? Math.floor(xMax / speed) : xMax;
- y = iy ? Math.floor(yMax / speed) : yMax;
- defaultView.scroll(x, y);
- if (inProgress) {
- requestID = requestAnimationFrame(requestScrollFrame);
- } else {
- typeof options.callback === "function" && options.callback();
- }
- };
- var cancelScrollFrame = function () {
- requestID && cancelAnimationFrame(requestID);
- };
- return {
- requestScrollFrame: requestScrollFrame,
- cancelScrollFrame: cancelScrollFrame,
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement