Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. /**
  2. * Singleton
  3. * @param {Object} [options]
  4. * @param {String} [options.axis] --- x | y | xy
  5. * @param {Number} [options.acceleration]
  6. * @param {Function} [options.callback]
  7. * @return {Object}
  8. */
  9. function scrollInitial(options) {
  10. options = Object(options);
  11. options.axis = options.axis || "y";
  12. options.acceleration = Math.abs(options.acceleration) || 0.1;
  13.  
  14. var requestID;
  15. var speed = 1 + options.acceleration;
  16. var defaultView = document.defaultView;
  17. var ix = options.axis === "x" || options.axis === "xy";
  18. var iy = options.axis === "y" || options.axis === "xy";
  19.  
  20. if (!ix && !iy) {
  21. throw new TypeError("Unexpected token");
  22. }
  23.  
  24. var requestScrollFrame = function () {
  25. var x1 = 0, y1 = 0;
  26. var x2 = 0, y2 = 0;
  27. var x3 = 0, y3 = 0;
  28. var x, y, xMax, yMax, inProgress;
  29.  
  30. if (document.body) {
  31. x1 = document.body.scrollLeft || 0;
  32. y1 = document.body.scrollTop || 0;
  33. }
  34.  
  35. if (document.documentElement) {
  36. x2 = document.documentElement.scrollLeft || 0;
  37. y2 = document.documentElement.scrollTop || 0;
  38. }
  39.  
  40. x3 = defaultView.scrollX || 0;
  41. y3 = defaultView.scrollY || 0;
  42.  
  43. xMax = Math.max(x1, x2, x3);
  44. yMax = Math.max(y1, y2, y3);
  45.  
  46. if (ix && iy) {
  47. inProgress = xMax > 0 || yMax > 0;
  48. } else if (ix) {
  49. inProgress = xMax > 0;
  50. } else if (iy) {
  51. inProgress = yMax > 0;
  52. }
  53.  
  54. x = ix ? Math.floor(xMax / speed) : xMax;
  55. y = iy ? Math.floor(yMax / speed) : yMax;
  56. defaultView.scroll(x, y);
  57.  
  58. if (inProgress) {
  59. requestID = requestAnimationFrame(requestScrollFrame);
  60. } else {
  61. typeof options.callback === "function" && options.callback();
  62. }
  63. };
  64.  
  65. var cancelScrollFrame = function () {
  66. requestID && cancelAnimationFrame(requestID);
  67. };
  68.  
  69.  
  70. return {
  71. requestScrollFrame: requestScrollFrame,
  72. cancelScrollFrame: cancelScrollFrame,
  73. };
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement