Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. (function (root) {
  2.  
  3. function __() {
  4.  
  5. var Animations = {
  6.  
  7. rotateX: function (pct, fromValue, toValue) {
  8. var deg = fromValue + ((toValue - fromValue) * pct);
  9. return {
  10. property: 'transform',
  11. value: 'rotateX(' + deg + 'deg)';
  12. }
  13. },
  14.  
  15. rotateY: function (pct, fromValue, toValue) {
  16. var deg = fromValue + ((toValue - fromValue) * pct);
  17. return {
  18. property: 'transform',
  19. value: 'rotateY(' + deg + 'deg)';
  20. }
  21. },
  22.  
  23. scale: function (pct, fromValue, toValue) {
  24. var num = fromValue + ((toValue - fromValue) * pct);
  25. return {
  26. property: 'transform',
  27. value: 'scale(' + num + ')';
  28. }
  29. }
  30.  
  31. };
  32.  
  33. var StyleValuesHandler = {
  34.  
  35. transform: function (values) {
  36. return values.join(' ');
  37. }
  38.  
  39. };
  40.  
  41. var Animation = function (el, action, options) {
  42. options = options || {};
  43. this.el = el;
  44.  
  45. if ('function' === typeof action) {
  46. this.action = action;
  47. } else {
  48. this.action = Animations[action];
  49. }
  50.  
  51. this.from = options.from || 0;
  52. delete options.from;
  53.  
  54. this.fromValue = options.fromValue;
  55. delete options.from;
  56.  
  57. this.to = options.to || 0;
  58. delete options.to;
  59.  
  60. this.toValue = options.toValue;
  61. delete options.toValue;
  62.  
  63. this.options = options;
  64. }
  65.  
  66. Animation.prototype = {
  67.  
  68. current: function (y) {
  69. var pct = (this.to - this.from) / y;
  70. return this.action(pct, this.fromValue, this.toValue, this.options);
  71. }
  72.  
  73. };
  74.  
  75. var Scroller = function () {
  76. this.animations = [];
  77. };
  78.  
  79. Scroller.prototype = {
  80.  
  81. animate: function (el, action, options) {
  82. var animation = new Animation(el, action, options);
  83. this.animations.push(animation);
  84. return this;
  85. },
  86.  
  87. bind: function (win) {
  88. return win.onScroll = function () {
  89. var y = win.pageYOffset, styles = {}, animation, current, el, property, value;
  90. for (var i=0; i<this.animations.length; i++) {
  91. animation = this.animations[i];
  92. if (animation.from >= y && animation.to <= y) {
  93. el = animation.el;
  94. current = animation.current(y);
  95. property = current.property;
  96. value = current.value;
  97. if (!styles[el]) styles[el] = {};
  98. if (!styles[el][property]) styles[el][property] = [];
  99. styles[el][property].push(value);
  100. }
  101. }
  102. var props, $el, value;
  103. for (var el in styles) {
  104. props = styles[el];
  105. $el = document.querySelector(el);
  106. for (var key in props) {
  107. value = (props[key].length > 1) ? StyleValuesHandler[key](props[key]) : props[key][0];
  108. $el.style[key] = value;
  109. }
  110. }
  111. }.bind(this));
  112. }
  113.  
  114. }
  115.  
  116. return Scroller;
  117.  
  118. };
  119.  
  120. if ('function' === typeof define && define.amd) {
  121. define(__);
  122. } else {
  123. root.Scroller = __();
  124. };
  125.  
  126. })(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement