****转载自自己发表于牛人部落专栏的文章****

一、前言

本文记录了自己利用原生javascript构建自己的动画库的过程,在不断改进的过程中,实现以下动画效果:

针对同一个dom元素上相继发生的动画,针对以下功能,尝试实现方案,(从一个元素向多个元素的拓展并不难,这里不做深入探究):

功能1.知道动画A和动画B的发生顺序(如A先发生,B后发生),能够按照代码撰写顺序实现动画A结束时,动画B调用

功能2.在满足功能1的基础上更进一步,当不知道动画A和动画B的发生顺序(如点击按钮1触发动画A,点击按钮2触发动画B,哪个按钮先点击不确定),能够达到1)两个动画不产生并发干扰;2)可以根据按钮的先后点击顺序,一个动画结束后另一个动画运行,即实现动画序列,以及动画的链式调用。

整个代码实现的过程,是不断改进的过程,包括:

1.利用requestAnimationFrame替代setTimeout来实现动画的平滑效果。

关于requestAnimationFrame的更多资料可参考这篇博客:http://www.zhangxinxu.com/wordpress/2013/09/css3-animation-requestanimationframe-tween-%E5%8A%A8%E7%94%BB%E7%AE%97%E6%B3%95/

2.尝试引入promise

关于promise的介绍可以参考此系列博客:https://github.com/wangfupeng1988/js-async-tutorial

3.尝试引入队列控制

队列结合running标识符来避免并发干扰;

二、相关辅助代码

以下是动画库实现的相关辅助代码,动画库的实现依赖于一下js文件,必须优先于动画库引入:

1.tween.js 实现各种缓动效果,具体可参见博客:http://www.zhangxinxu.com/wordpress/2016/12/how-use-tween-js-animation-easing/

代码如下:

  1. /**
  2. *Tween 缓动相关
  3. */
  4. var tween = {
  5. Linear: function(t, b, c, d) {
  6. return c * t / d + b;
  7. },
  8. Quad: {
  9. easeIn: function(t, b, c, d) {
  10. return c * (t /= d) * t + b;
  11. },
  12. easeOut: function(t, b, c, d) {
  13. return -c * (t /= d) * (t - ) + b;
  14. },
  15. easeInOut: function(t, b, c, d) {
  16. if ((t /= d / ) < ) return c / * t * t + b;
  17. return -c / * ((--t) * (t - ) - ) + b;
  18. }
  19. },
  20. Cubic: {
  21. easeIn: function(t, b, c, d) {
  22. return c * (t /= d) * t * t + b;
  23. },
  24. easeOut: function(t, b, c, d) {
  25. return c * ((t = t / d - ) * t * t + ) + b;
  26. },
  27. easeInOut: function(t, b, c, d) {
  28. if ((t /= d / ) < ) return c / * t * t * t + b;
  29. return c / * ((t -= ) * t * t + ) + b;
  30. }
  31. },
  32. Quart: {
  33. easeIn: function(t, b, c, d) {
  34. return c * (t /= d) * t * t * t + b;
  35. },
  36. easeOut: function(t, b, c, d) {
  37. return -c * ((t = t / d - ) * t * t * t - ) + b;
  38. },
  39. easeInOut: function(t, b, c, d) {
  40. if ((t /= d / ) < ) return c / * t * t * t * t + b;
  41. return -c / * ((t -= ) * t * t * t - ) + b;
  42. }
  43. },
  44. Quint: {
  45. easeIn: function(t, b, c, d) {
  46. return c * (t /= d) * t * t * t * t + b;
  47. },
  48. easeOut: function(t, b, c, d) {
  49. return c * ((t = t / d - ) * t * t * t * t + ) + b;
  50. },
  51. easeInOut: function(t, b, c, d) {
  52. if ((t /= d / ) < ) return c / * t * t * t * t * t + b;
  53. return c / * ((t -= ) * t * t * t * t + ) + b;
  54. }
  55. },
  56. Sine: {
  57. easeIn: function(t, b, c, d) {
  58. return -c * Math.cos(t / d * (Math.PI / )) + c + b;
  59. },
  60. easeOut: function(t, b, c, d) {
  61. return c * Math.sin(t / d * (Math.PI / )) + b;
  62. },
  63. easeInOut: function(t, b, c, d) {
  64. return -c / * (Math.cos(Math.PI * t / d) - ) + b;
  65. }
  66. },
  67. Expo: {
  68. easeIn: function(t, b, c, d) {
  69. return (t == ) ? b : c * Math.pow(, * (t / d - )) + b;
  70. },
  71. easeOut: function(t, b, c, d) {
  72. return (t == d) ? b + c : c * (-Math.pow(, - * t / d) + ) + b;
  73. },
  74. easeInOut: function(t, b, c, d) {
  75. if (t == ) return b;
  76. if (t == d) return b + c;
  77. if ((t /= d / ) < ) return c / * Math.pow(, * (t - )) + b;
  78. return c / * (-Math.pow(, - * --t) + ) + b;
  79. }
  80. },
  81. Circ: {
  82. easeIn: function(t, b, c, d) {
  83. return -c * (Math.sqrt( - (t /= d) * t) - ) + b;
  84. },
  85. easeOut: function(t, b, c, d) {
  86. return c * Math.sqrt( - (t = t / d - ) * t) + b;
  87. },
  88. easeInOut: function(t, b, c, d) {
  89. if ((t /= d / ) < ) return -c / * (Math.sqrt( - t * t) - ) + b;
  90. return c / * (Math.sqrt( - (t -= ) * t) + ) + b;
  91. }
  92. },
  93. Elastic: {
  94. easeIn: function(t, b, c, d, a, p) {
  95. if (t == ) return b;
  96. if ((t /= d) == ) return b + c;
  97. if (!p) p = d * .;
  98. if (!a || a < Math.abs(c)) {
  99. a = c;
  100. var s = p / ;
  101. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  102. return -(a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
  103. },
  104. easeOut: function(t, b, c, d, a, p) {
  105. if (t == ) return b;
  106. if ((t /= d) == ) return b + c;
  107. if (!p) p = d * .;
  108. if (!a || a < Math.abs(c)) {
  109. a = c;
  110. var s = p / ;
  111. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  112. return (a * Math.pow(, - * t) * Math.sin((t * d - s) * ( * Math.PI) / p) + c + b);
  113. },
  114. easeInOut: function(t, b, c, d, a, p) {
  115. if (t == ) return b;
  116. if ((t /= d / ) == ) return b + c;
  117. if (!p) p = d * (. * 1.5);
  118. if (!a || a < Math.abs(c)) {
  119. a = c;
  120. var s = p / ;
  121. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  122. if (t < ) return -. * (a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
  123. return a * Math.pow(, - * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p) * . + c + b;
  124. }
  125. },
  126. Back: {
  127. easeIn: function(t, b, c, d, s) {
  128. if (s == undefined) s = 1.70158;
  129. return c * (t /= d) * t * ((s + ) * t - s) + b;
  130. },
  131. easeOut: function(t, b, c, d, s) {
  132. if (s == undefined) s = 1.70158;
  133. return c * ((t = t / d - ) * t * ((s + ) * t + s) + ) + b;
  134. },
  135. easeInOut: function(t, b, c, d, s) {
  136. if (s == undefined) s = 1.70158;
  137. if ((t /= d / ) < ) return c / * (t * t * (((s *= (1.525)) + ) * t - s)) + b;
  138. return c / * ((t -= ) * t * (((s *= (1.525)) + ) * t + s) + ) + b;
  139. }
  140. },
  141. Bounce: {
  142. easeIn: function(t, b, c, d) {
  143. return c - Tween.Bounce.easeOut(d - t, , c, d) + b;
  144. },
  145. easeOut: function(t, b, c, d) {
  146. if ((t /= d) < ( / 2.75)) {
  147. return c * (7.5625 * t * t) + b;
  148. } else if (t < ( / 2.75)) {
  149. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .) + b;
  150. } else if (t < (2.5 / 2.75)) {
  151. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .) + b;
  152. } else {
  153. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .) + b;
  154. }
  155. },
  156. easeInOut: function(t, b, c, d) {
  157. if (t < d / ) return Tween.Bounce.easeIn(t * , , c, d) * . + b;
  158. else return Tween.Bounce.easeOut(t * - d, , c, d) * . + c * . + b;
  159. }
  160. }
  161. };

2.辅助工具util.js,其中包括样式获取和设置的方法,以及requestAnimationFrame,cancelAnimationFrame,获取当前时间戳兼容的方法

  1. //获取元素属性
  2. //元素属性都按照整数计算
  3. var getStyle = function(dom, prop) {
  4. if (prop === 'opacity' && dom.style.filter) {
  5. return window.style.filter.match(/(\d+)/)[];
  6. }
  7. var tmp = window.getComputedStyle ? window.getComputedStyle(dom, null)[prop] : dom.currentStyle[prop];
  8. return prop === 'opacity' ? parseFloat(tmp, ) : parseInt(tmp, );
  9. };
  10. //设置元素属性
  11. var setStyle = function(dom, prop, value) {
  12. if (prop === 'opacity') {
  13. dom.style.filter = '(opacity(' + parseFloat(value / ) + '))';
  14. dom.style.opacity = value;
  15. return;
  16. }
  17. dom.style[prop] = parseInt(value, ) + 'px';
  18. };
  19.  
  20. //requestAnimationFrame的兼容处理
  21. (function() {
  22. var lastTime = ;
  23. var vendors = ['webkit', 'moz'];
  24. for (var x = ; x < vendors.length && !window.requestAnimationFrame; ++x) {
  25. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  26. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||
  27. window[vendors[x] + 'CancelRequestAnimationFrame'];
  28. }
  29.  
  30. if (!window.requestAnimationFrame) {
  31. window.requestAnimationFrame = function(callback, element) {
  32. var currTime = new Date().getTime();
  33. var timeToCall = Math.max(, 16.7 - (currTime - lastTime));
  34. var id = window.setTimeout(function() {
  35. callback(currTime + timeToCall);
  36. }, timeToCall);
  37. lastTime = currTime + timeToCall;
  38. return id;
  39. };
  40. }
  41. if (!window.cancelAnimationFrame) {
  42. window.cancelAnimationFrame = function(id) {
  43. clearTimeout(id);
  44. };
  45. }
  46. }());
  47.  
  48. //时间戳获取的兼容处理
  49. function nowtime() {
  50. if (typeof performance !== 'undefined' && performance.now) {
  51. return performance.now();
  52. }
  53. return Date.now ? Date.now() : (new Date()).getTime();
  54. }

3.为了便于测试,布局html文件如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>测试动画库</title>
  7. <style>
  8. .mydiv {
  9. width: 300px;
  10. height: 200px;
  11. background-color: pink;
  12. position: absolute;
  13. top: 100px;
  14. left: 100px;
  15. }
  16. </style>
  17. </head>
  18.  
  19. <body>
  20. <div class="mydiv" id="mydiv"></div>
  21. </body>
  22. </html>

三、动画库animation的具体实现

1.仅考虑实现功能1:即

知道动画A和动画B的发生顺序(如A先发生,B后发生),能够按照代码撰写顺序实现动画A结束时,动画B调用

方法一:利用动画结束时,执行回调的思路,代码如下:

  1. //实现动画库(暂不使用promise)
  2. var Animate = {
  3. init: function(el) {
  4. this.el = typeof el === 'string' ? document.querySelector(el) : el;
  5. this.timer = null;
  6. return this;
  7. },
  8. initAnim: function(props, option) {
  9. this.propChange = {};
  10. this.duration = (option && option.duration) || ;
  11. this.easing = (option && option.easing) || tween.Linear;
  12. for (var prop in props) {
  13. this.propChange[prop] = {};
  14. this.propChange[prop]['to'] = props[prop];
  15. this.propChange[prop]['from'] = getStyle(this.el, prop);
  16. }
  17. return this;
  18. },
  19. stop: function() {
  20. clearTimeout(this.timer);
  21. this.timer = null;
  22. return this;
  23. },
  24. play: function(callback) {
  25. var startTime = ;
  26. var self = this;
  27. if (this.timer) {
  28. this.stop();
  29. }
  30.  
  31. function step() {
  32. if (!startTime) {
  33. startTime = nowtime();
  34. }
  35. var passedTime = Math.min(nowtime() - startTime, self.duration);
  36. console.log('passedTime:' + passedTime + ',duration:' + self.duration);
  37. for (var prop in self.propChange) {
  38. var target = self.easing(passedTime, self.propChange[prop]['from'], self.propChange[prop]['to'] - self.propChange[prop]['from'], self.duration);
  39. setStyle(self.el, prop, target);
  40. }
  41. if (passedTime >= self.duration) {
  42. self.stop();
  43. if (callback) {
  44. callback.call(self);
  45. }
  46. } else {
  47. this.timer = setTimeout(step, / );
  48. }
  49. }
  50. this.timer = setTimeout(step, / );
  51. },
  52. runAnim: function(props, option, callback) {
  53. this.initAnim(props, option);
  54. this.play(callback);
  55. }
  56. };

调用代码如下:

  1. <script type="text/javascript">
  2. //测试animate.js
  3. //利用回调来实现顺序调用
  4. var div = document.getElementById('mydiv');
  5. var anim = Object.create(Animate);
  6. anim.init(div);
  7. anim.runAnim({
  8. width:
  9. }, {
  10. duration:
  11. }, function() {
  12. anim.runAnim({
  13. height:
  14. }, {
  15. duration:
  16. });
  17. });

经过测试,上述代码能够实现,长度变为500之后,高度再变为500.即实现了功能1.

但是,如果两个动画发生的先后顺序实现并不知道,如点击按钮1使得长度变为500,紧接着点击按钮2使得高度变为500,后者反过来。总之哪个按钮先按下并不知情。这种情况,上面的方法就不适用了。程序永远只执行最后一个动画事件,因为一旦进入动画执行函数play,就首先将上一个函数的timer进行了清空。

方法二:如果只是单纯的实现功能,除了动画完成执行回调的思路外,自然而然可以考虑到将回调的写法改进为promise的写法,此外下面的代码还使用requestAnimation替代了setTimeout.具体如下:

  1. //实现动画库
  2. //1.使用requestAnimationFrame
  3. //2.引入promise
  4. var Animate = {
  5. init: function(el) {
  6. this.el = typeof el === 'string' ? document.querySelector(el) : el;
  7. this.reqId = null;
  8. return this;
  9. },
  10. initAnim: function(props, option) {
  11. this.propChange = {};
  12. this.duration = (option && option.duration) || ;
  13. this.easing = (option && option.easing) || tween.Linear;
  14. for (var prop in props) {
  15. this.propChange[prop] = {};
  16. this.propChange[prop]['to'] = props[prop];
  17. this.propChange[prop]['from'] = getStyle(this.el, prop);
  18. }
  19. return this;
  20. },
  21. stop: function() {
  22. if (this.reqId) {
  23. cancelAnimationFrame(this.reqId);
  24. }
  25. this.reqId = null;
  26. return this;
  27. },
  28. play: function() {
  29. console.log('进入动画:');
  30. var startTime = ;
  31. var self = this;
  32. if (this.reqId) {
  33. this.stop();
  34. }
  35. return new Promise((resolve, reject) => {
  36. function step(timestamp) {
  37. if (!startTime) {
  38. startTime = timestamp;
  39. }
  40. var passedTime = Math.min(timestamp - startTime, self.duration);
  41. console.log('passedTime:' + passedTime + ',duration:' + self.duration);
  42. for (var prop in self.propChange) {
  43. var target = self.easing(passedTime, self.propChange[prop]['from'], self.propChange[prop]['to'] - self.propChange[prop]['from'], self.duration);
  44. setStyle(self.el, prop, target);
  45. }
  46. if (passedTime >= self.duration) {
  47. self.stop();
  48. resolve();
  49. } else {
  50. this.reqId = requestAnimationFrame(step);
  51. }
  52. }
  53. this.reqId = requestAnimationFrame(step);
  54. this.cancel = function() {
  55. self.stop();
  56. reject('cancel');
  57. };
  58. });
  59.  
  60. },
  61. runAnim: function(props, option) {
  62. this.initAnim(props, option);
  63. return this.play();
  64. }
  65. };

调用方法如下:

1.可以使用promise的then方法:

  1. var div = document.getElementById('mydiv');
  2. var anim = Object.create(Animate);
  3. anim.init(div);
  4. anim.runAnim({width:},{duration:}).then(function(){
  5. return anim.runAnim({height:},{duration:});
  6. }).then(function(){
  7. console.log('end');
  8. });

2.当然也可以使用ES7新引入的async,await方法(目前chrome浏览器已经支持)

  1. var div = document.getElementById('mydiv');
  2. var anim = Object.create(Animate);
  3. anim.init(div);
  4.  
  5. async function run() {
  6. var a = await anim.runAnim({
  7. width: ,
  8. opacity: .
  9. }, {
  10. duration:
  11. });
  12. var b = await anim.runAnim({
  13. height:
  14. }, {
  15. duration:
  16. });
  17. }
  18. run();

这种方法同样存在一样的弊端,即只适用于动画顺序实现知道的情形。

2.考虑功能2的情形,即动画发生顺序实现无法预知的情况下,在一个动画进行过程中触发另一个不会引发冲突,而是根据触发顺序依次执行。

实现思路:既然是依次,就容易想到队列,同时需要设置标志位running,保证在动画进行过程中,不会触发出队事件。

具体如下:

  1. //实现动画库
  2. //改进:利用requestAnimationFrame替代setTimeout
  3. var Animate = {
  4. init: function(el) {
  5. this.el = typeof el === 'string' ? document.querySelector(el) : el;
  6. this.queue = [];
  7. this.running = false;
  8. this.reqId = null;
  9. return this;
  10. },
  11. initAnim: function(props, option) {
  12. this.propChange = {};
  13. this.duration = (option && option.duration) || ;
  14. this.easing = (option && option.easing) || tween.Linear;
  15. for (var prop in props) {
  16. this.propChange[prop] = {};
  17. this.propChange[prop]['to'] = props[prop];
  18. this.propChange[prop]['from'] = getStyle(this.el, prop);
  19. }
  20. return this;
  21. },
  22. stop: function() {
  23. this.running = false;
  24. if (this.reqId) {
  25. cancelAnimationFrame(this.reqId);
  26. }
  27. this.reqId = null;
  28. return this;
  29. },
  30. play: function() {
  31. this.running = true;
  32. console.log('进入动画:' + this.running);
  33. var startTime = ;
  34. var self = this;
  35. if (this.reqId) {
  36. this.stop();
  37. }
  38.  
  39. function step(timestamp) {
  40. if (!startTime) {
  41. startTime = timestamp;
  42. }
  43. var passedTime = Math.min(timestamp - startTime, self.duration);
  44. console.log('passedTime:' + passedTime + ',duration:' + self.duration);
  45. for (var prop in self.propChange) {
  46. var target = self.easing(passedTime, self.propChange[prop]['from'], self.propChange[prop]['to'] - self.propChange[prop]['from'], self.duration);
  47. setStyle(self.el, prop, target);
  48. }
  49. if (passedTime >= self.duration) {
  50. self.stop();
  51. //播放队列当中的下一组动画
  52. self.dequeue();
  53. } else {
  54. this.reqId = requestAnimationFrame(step, / );
  55. }
  56. }
  57. this.reqId = requestAnimationFrame(step, / );
  58. },
  59. enqueue: function(props, option) {
  60. this.queue.push(() => {
  61. this.initAnim.call(this, props, option);
  62. this.play.call(this);
  63. });
  64. return this;
  65. },
  66. hasNext: function() {
  67. return this.queue.length > ;
  68. },
  69. dequeue: function(props) {
  70. //console.log('length', this.queue.length);
  71. if (!this.running && this.hasNext()) {
  72. if (props) {
  73. for (var prop in props) {
  74. console.log(prop + '出队成功');
  75. }
  76. }
  77. //console.log('length',this.queue.length);
  78. this.queue.shift().call(this);
  79. }
  80. return this;
  81. },
  82. runAnim: function(props, option) {
  83. this.enqueue(props, option);
  84. //传入参数props仅仅是为了调试打印,即使不传也不影响功能
  85. this.dequeue(props);
  86. //setTimeout(this.dequeue.bind(this), 0);
  87. }
  88. };

测试方法如下:

  1. //测试animate2.js
  2. //使用requeustAnimationFrame代替settimeout实现动画库
  3. var div = document.getElementById('mydiv');
  4. var anim = Object.create(Animate);
  5. anim.init(div);
  6. anim.runAnim({
  7. width: ,
  8. opacity: .
  9. }, {
  10. duration:
  11. });
  12. anim.runAnim({
  13. height:
  14. }, {
  15. duration:
  16. });

2,考虑能否将promise与队列结合起来,于是有了下面的代码:

  1. //实现动画库
  2. //1.使用requestAnimationFrame
  3. //2.引入promise
  4. var Animate = {
  5. init: function(el) {
  6. this.el = typeof el === 'string' ? document.querySelector(el) : el;
  7. this.reqId = null;
  8. this.queue = [];
  9. this.running = false;
  10. return this;
  11. },
  12. initAnim: function(props, option) {
  13. this.propChange = {};
  14. this.duration = (option && option.duration) || ;
  15. this.easing = (option && option.easing) || tween.Linear;
  16. for (var prop in props) {
  17. this.propChange[prop] = {};
  18. this.propChange[prop]['to'] = props[prop];
  19. this.propChange[prop]['from'] = getStyle(this.el, prop);
  20. }
  21. return this;
  22. },
  23. stop: function() {
  24. if (this.reqId) {
  25. cancelAnimationFrame(this.reqId);
  26. }
  27. this.running = false;
  28. this.reqId = null;
  29. return this;
  30. },
  31. play: function() {
  32. this.running = true;
  33. console.log('进入动画:' + this.running);
  34. var startTime = ;
  35. var self = this;
  36. if (this.reqId) {
  37. this.stop();
  38. }
  39. return new Promise((resolve, reject) => {
  40. function step(timestamp) {
  41. if (!startTime) {
  42. startTime = timestamp;
  43. }
  44. var passedTime = Math.min(timestamp - startTime, self.duration);
  45. console.log('passedTime:' + passedTime + ',duration:' + self.duration);
  46. for (var prop in self.propChange) {
  47. var target = self.easing(passedTime, self.propChange[prop]['from'], self.propChange[prop]['to'] - self.propChange[prop]['from'], self.duration);
  48. setStyle(self.el, prop, target);
  49. }
  50. if (passedTime >= self.duration) {
  51. self.stop();
  52. self.dequeue();
  53. resolve();
  54.  
  55. } else {
  56. this.reqId = requestAnimationFrame(step);
  57. }
  58. }
  59. this.reqId = requestAnimationFrame(step);
  60. this.cancel = function() {
  61. self.stop();
  62. reject('cancel');
  63. };
  64. });
  65.  
  66. },
  67. hasNext: function() {
  68. return this.queue.length > ;
  69. },
  70. enqueue: function(props, option) {
  71. this.queue.push(() => {
  72. this.initAnim(props, option);
  73. return this.play();
  74. });
  75. },
  76. dequeue: function(callback) {
  77. var prom;
  78. if (!this.running && this.hasNext()) {
  79. prom = this.queue.shift().call(this);
  80. }
  81. if (callback) {
  82. return prom.then(() => {
  83. callback.call(this);
  84. });
  85. } else {
  86. return prom;
  87. }
  88. },
  89. runAnim(props, option, callback) {
  90. this.enqueue(props, option);
  91. this.dequeue(callback);
  92. }
  93. };

不过感觉这么做意义不是特别大。动画队列中的每一个元素是个函数,该函数返回一个promise,貌似看起来是为给动画队列中每一个动画结束的时候添加回调增加了可能,经过如下测试:

  1. var div = document.getElementById('mydiv');
  2. var anim = Object.create(Animate);
  3. anim.init(div);
  4. anim.runAnim({
  5. width:
  6. }, {
  7. duration:
  8. }, function() {
  9. console.log();
  10. });
  11. anim.runAnim({
  12. height:
  13. }, {

如果回调是个同步代码,如上面的console.log(1),那么该打印语句在宽度变为500动画结束后立即执行。

但如果回调是个异步代码,如下:

  1. var div = document.getElementById('mydiv');
  2. var anim = Object.create(Animate);
  3. anim.init(div);
  4. anim.runAnim({
  5. width:
  6. }, {
  7. duration:
  8. }, function() {
  9. anim.runAnim({
  10. opacity: .
  11. });
  12. });
  13. anim.runAnim({
  14. height:
  15. }, {
  16. duration:
  17. });

发现透明度的变化,实在长度变为500,并且高度变为500的动画结束之后,才执行。

总结:

1.回调与promise的关系无需多说,通过上面的代码发现二者和队列貌似也有某种联系。转念一想,貌似jquery中的defer,promise就是回调和队列结合实现的

2.上面的代码库远不完善,很多因素没有考虑,诸如多元素动画,css3动画等等。希望后续有时间能够多多优化。

二、封装javascript动画库2

参照jQuery队列设计方法,不是通过变量running判定动画是否正在执行,而是通过队列队首元素run来控制,此外还支持:

1)预定义动画序列;

2)直接到达动画最后一帧;

3)动画反转;

4)预定义动画效果。

工具类util.js

  1. //获取元素属性
  2. //返回元素对应的属性值(不包含单位)
  3. //考虑的特殊情况包括:
  4. //1.透明度,值为小数,如0.2
  5. //2.颜色,值的表示法有rgb,16进制表示法(缩写,不缩写。两种形式)
  6. //3.transform属性,包括 [ "translateZ", "scale", "scaleX", "scaleY", "translateX", "translateY", "scaleZ", "skewX", "skewY", "rotateX", "rotateY", "rotateZ" ]
  7. //transfrom属性中,不考虑matrix,translate(30,40),translate3d等复合写法
  8. // 上面的功能尚未实现,等有时间补上
  9. (function(window) {
  10. var transformPropNames = ["translateZ", "scale", "scaleX", "scaleY", "translateX", "translateY", "scaleZ", "skewX", "skewY", "rotateX", "rotateY", "rotateZ"];
  11.  
  12. window.getStyle = function(dom, prop) {
  13. var tmp = window.getComputedStyle ? window.getComputedStyle(dom, null)[prop] : dom.currentStyle[prop];
  14. return prop === 'opacity' ? parseFloat(tmp, ) : parseInt(tmp, );
  15. };
  16. //设置元素属性
  17. window.setStyle = function(dom, prop, value) {
  18. if (prop === 'opacity') {
  19. dom.style.filter = '(opacity(' + parseFloat(value * ) + '))';
  20. dom.style.opacity = value;
  21. return;
  22. }
  23. dom.style[prop] = parseInt(value, ) + 'px';
  24. };
  25. })(window);
  26.  
  27. //requestAnimationFrame的兼容处理
  28. (function() {
  29. var lastTime = ;
  30. var vendors = ['webkit', 'moz'];
  31. for (var x = ; x < vendors.length && !window.requestAnimationFrame; ++x) {
  32. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  33. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||
  34. window[vendors[x] + 'CancelRequestAnimationFrame'];
  35. }
  36.  
  37. if (!window.requestAnimationFrame) {
  38. window.requestAnimationFrame = function(callback, element) {
  39. var currTime = new Date().getTime();
  40. var timeToCall = Math.max(, 16.7 - (currTime - lastTime));
  41. var id = window.setTimeout(function() {
  42. callback(currTime + timeToCall);
  43. }, timeToCall);
  44. lastTime = currTime + timeToCall;
  45. return id;
  46. };
  47. }
  48. if (!window.cancelAnimationFrame) {
  49. window.cancelAnimationFrame = function(id) {
  50. clearTimeout(id);
  51. };
  52. }
  53. }());
  54.  
  55. //时间戳获取的兼容处理
  56. function nowtime() {
  57. if (typeof performance !== 'undefined' && performance.now) {
  58. return performance.now();
  59. }
  60. return Date.now ? Date.now() : (new Date()).getTime();
  61. }

util.js

缓动效果:tween.js

  1. /**
  2. *Tween 缓动相关
  3. */
  4. var tween = {
  5. Linear: function(t, b, c, d) {
  6. return c * t / d + b;
  7. },
  8. Quad: {
  9. easeIn: function(t, b, c, d) {
  10. return c * (t /= d) * t + b;
  11. },
  12. easeOut: function(t, b, c, d) {
  13. return -c * (t /= d) * (t - ) + b;
  14. },
  15. easeInOut: function(t, b, c, d) {
  16. if ((t /= d / ) < ) return c / * t * t + b;
  17. return -c / * ((--t) * (t - ) - ) + b;
  18. }
  19. },
  20. Cubic: {
  21. easeIn: function(t, b, c, d) {
  22. return c * (t /= d) * t * t + b;
  23. },
  24. easeOut: function(t, b, c, d) {
  25. return c * ((t = t / d - ) * t * t + ) + b;
  26. },
  27. easeInOut: function(t, b, c, d) {
  28. if ((t /= d / ) < ) return c / * t * t * t + b;
  29. return c / * ((t -= ) * t * t + ) + b;
  30. }
  31. },
  32. Quart: {
  33. easeIn: function(t, b, c, d) {
  34. return c * (t /= d) * t * t * t + b;
  35. },
  36. easeOut: function(t, b, c, d) {
  37. return -c * ((t = t / d - ) * t * t * t - ) + b;
  38. },
  39. easeInOut: function(t, b, c, d) {
  40. if ((t /= d / ) < ) return c / * t * t * t * t + b;
  41. return -c / * ((t -= ) * t * t * t - ) + b;
  42. }
  43. },
  44. Quint: {
  45. easeIn: function(t, b, c, d) {
  46. return c * (t /= d) * t * t * t * t + b;
  47. },
  48. easeOut: function(t, b, c, d) {
  49. return c * ((t = t / d - ) * t * t * t * t + ) + b;
  50. },
  51. easeInOut: function(t, b, c, d) {
  52. if ((t /= d / ) < ) return c / * t * t * t * t * t + b;
  53. return c / * ((t -= ) * t * t * t * t + ) + b;
  54. }
  55. },
  56. Sine: {
  57. easeIn: function(t, b, c, d) {
  58. return -c * Math.cos(t / d * (Math.PI / )) + c + b;
  59. },
  60. easeOut: function(t, b, c, d) {
  61. return c * Math.sin(t / d * (Math.PI / )) + b;
  62. },
  63. easeInOut: function(t, b, c, d) {
  64. return -c / * (Math.cos(Math.PI * t / d) - ) + b;
  65. }
  66. },
  67. Expo: {
  68. easeIn: function(t, b, c, d) {
  69. return (t == ) ? b : c * Math.pow(, * (t / d - )) + b;
  70. },
  71. easeOut: function(t, b, c, d) {
  72. return (t == d) ? b + c : c * (-Math.pow(, - * t / d) + ) + b;
  73. },
  74. easeInOut: function(t, b, c, d) {
  75. if (t == ) return b;
  76. if (t == d) return b + c;
  77. if ((t /= d / ) < ) return c / * Math.pow(, * (t - )) + b;
  78. return c / * (-Math.pow(, - * --t) + ) + b;
  79. }
  80. },
  81. Circ: {
  82. easeIn: function(t, b, c, d) {
  83. return -c * (Math.sqrt( - (t /= d) * t) - ) + b;
  84. },
  85. easeOut: function(t, b, c, d) {
  86. return c * Math.sqrt( - (t = t / d - ) * t) + b;
  87. },
  88. easeInOut: function(t, b, c, d) {
  89. if ((t /= d / ) < ) return -c / * (Math.sqrt( - t * t) - ) + b;
  90. return c / * (Math.sqrt( - (t -= ) * t) + ) + b;
  91. }
  92. },
  93. Elastic: {
  94. easeIn: function(t, b, c, d, a, p) {
  95. if (t == ) return b;
  96. if ((t /= d) == ) return b + c;
  97. if (!p) p = d * .;
  98. if (!a || a < Math.abs(c)) {
  99. a = c;
  100. var s = p / ;
  101. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  102. return -(a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
  103. },
  104. easeOut: function(t, b, c, d, a, p) {
  105. if (t == ) return b;
  106. if ((t /= d) == ) return b + c;
  107. if (!p) p = d * .;
  108. if (!a || a < Math.abs(c)) {
  109. a = c;
  110. var s = p / ;
  111. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  112. return (a * Math.pow(, - * t) * Math.sin((t * d - s) * ( * Math.PI) / p) + c + b);
  113. },
  114. easeInOut: function(t, b, c, d, a, p) {
  115. if (t == ) return b;
  116. if ((t /= d / ) == ) return b + c;
  117. if (!p) p = d * (. * 1.5);
  118. if (!a || a < Math.abs(c)) {
  119. a = c;
  120. var s = p / ;
  121. } else var s = p / ( * Math.PI) * Math.asin(c / a);
  122. if (t < ) return -. * (a * Math.pow(, * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p)) + b;
  123. return a * Math.pow(, - * (t -= )) * Math.sin((t * d - s) * ( * Math.PI) / p) * . + c + b;
  124. }
  125. },
  126. Back: {
  127. easeIn: function(t, b, c, d, s) {
  128. if (s == undefined) s = 1.70158;
  129. return c * (t /= d) * t * ((s + ) * t - s) + b;
  130. },
  131. easeOut: function(t, b, c, d, s) {
  132. if (s == undefined) s = 1.70158;
  133. return c * ((t = t / d - ) * t * ((s + ) * t + s) + ) + b;
  134. },
  135. easeInOut: function(t, b, c, d, s) {
  136. if (s == undefined) s = 1.70158;
  137. if ((t /= d / ) < ) return c / * (t * t * (((s *= (1.525)) + ) * t - s)) + b;
  138. return c / * ((t -= ) * t * (((s *= (1.525)) + ) * t + s) + ) + b;
  139. }
  140. },
  141. Bounce: {
  142. easeIn: function(t, b, c, d) {
  143. return c - Tween.Bounce.easeOut(d - t, , c, d) + b;
  144. },
  145. easeOut: function(t, b, c, d) {
  146. if ((t /= d) < ( / 2.75)) {
  147. return c * (7.5625 * t * t) + b;
  148. } else if (t < ( / 2.75)) {
  149. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .) + b;
  150. } else if (t < (2.5 / 2.75)) {
  151. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .) + b;
  152. } else {
  153. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .) + b;
  154. }
  155. },
  156. easeInOut: function(t, b, c, d) {
  157. if (t < d / ) return Tween.Bounce.easeIn(t * , , c, d) * . + b;
  158. else return Tween.Bounce.easeOut(t * - d, , c, d) * . + c * . + b;
  159. }
  160. }
  161. };

tween.js

具体实现animation.js

  1. var Animate = {
  2. init: function(el) {
  3. this.dom = typeof el === 'string' ? document.querySelector(el) : el;
  4. // console.log(this.dom);
  5. this.queue = [];
  6. this.isRuning = false;
  7. this.reqId = null;
  8. this.toEnd = false;
  9. },
  10. initAnim: function(props, opts) {
  11. this.propchanges = {};
  12. this.duration = (opts && opts.duration) || ;
  13. this.easing = (opts && opts.easing) || tween.Linear;
  14. //为了实现reverse,需要initProps来记录变化之前的数值
  15. this.initprops = {};
  16. // 可以使用数组同时指定开始值和结束值,也可以仅仅指定结束值
  17. for (var prop in props) {
  18. this.propchanges[prop] = {};
  19. if (Array.isArray(props[prop])) {
  20. this.propchanges[prop]['from'] = this.initprops[prop] = props[prop][];
  21. this.propchanges[prop]['to'] = props[prop][];
  22. } else {
  23. this.propchanges[prop]['from'] = this.initprops[prop] = getStyle(this.dom, prop);
  24. this.propchanges[prop]['to'] = props[prop];
  25. }
  26. }
  27. return this;
  28. },
  29. stop: function() {
  30. this.isRuning = false;
  31. if (this.reqId) {
  32. cancelAnimationFrame(this.reqId);
  33. this.reqId = null;
  34. }
  35. return this;
  36. },
  37. play: function(opts) {
  38. console.log('opts', opts);
  39. this.isRuning = true;
  40. var self = this;
  41. var startTime;
  42.  
  43. function tick(timestamp) {
  44. var curTime = timestamp || nowtime();
  45. if (!startTime) {
  46. startTime = curTime;
  47. }
  48. // console.log('passedTime', curTime - startTime);
  49. var passedTime = Math.min(curTime - startTime, self.duration);
  50. // 实现finish功能,直接到达动画最终状态
  51. if (self.toEnd) {
  52. passedTime = self.duration;
  53. }
  54. for (var prop in self.propchanges) {
  55. var curValue = self.easing(passedTime, self.propchanges[prop]['from'], self.propchanges[prop]['to'] - self.propchanges[prop]['from'], self.duration);
  56. console.log(prop + ':' + passedTime, curValue);
  57. setStyle(self.dom, prop, curValue);
  58. }
  59. if (passedTime >= self.duration) {
  60. //动画停止
  61. self.stop(); //在stop中将isRunning置为了false
  62. // startTime = 0;
  63. //下一个动画出队
  64. self.dequeue();
  65. if (opts.next) {
  66. opts.next.call(null);
  67. }
  68. } else if (self.isRuning) {
  69. self.reqId = requestAnimationFrame(tick);
  70. }
  71.  
  72. //必须将判断放在else里面
  73. //否则经过试验,链式调用时,除了第一个动画外,其他动画会出现问题
  74. //这是因为,虽然stop中将isRunning置为了false
  75. //但是接下来的dequeue执行play,又马上将isRunning置为了true
  76. // if (self.isRuning) {
  77. // self.reqId = requestAnimationFrame(tick);
  78. // }
  79. }
  80. tick();
  81. return this;
  82. },
  83. // 如果当前有动画正在执行,那么动画队列的首个元素一定是'run'
  84. // 动画函数出队之后,开始执行前,立即在队列头部添加一个'run'元素,代表动画函数正在执行
  85. // 只有当对应动画函数执行完之后,才会调用出队操作,原队首的'run'元素才可以出队
  86. // 如果动画函数执行完毕,调用出队操作之后,动画队列中还有下一个动画函数,下一个动画函数出队后,执行之前,依旧将队列头部置为'run',重复上述操作
  87. // 如果动画函数执行完毕,调用出队操作之后,动画队列中没有其他动画函数,那么队首的‘run’元素出队之后,队列为空
  88. // 首次入队时,动画队列的首个元素不是'run',动画立即出队执行
  89. //
  90. enqueue: function(fn) {
  91. this.queue.push(fn);
  92. if (this.queue[] !== 'run') {
  93. this.dequeue();
  94. }
  95. },
  96. //上一个版本使用isRuning来控制出队执行的时机,这里运用队首的'run'来控制,isRunning的一一貌似不大
  97. dequeue: function() {
  98. while (this.queue.length) {
  99. var curItem = this.queue.shift();
  100. if (typeof curItem === 'function') {
  101. curItem.call(this); //这是个异步操作
  102. this.queue.unshift('run');
  103. break;
  104. }
  105. }
  106. },
  107. // 对外接口:开始动画的入口函数
  108. animate: function(props, opts) {
  109. // console.log(typeof this.queue);
  110. this.enqueue(() => {
  111. this.initAnim(props, opts);
  112. this.play(opts);
  113. });
  114. return this;
  115. },
  116.  
  117. // 对外接口,直接到达动画的最终状态
  118. finish: function() {
  119. this.toEnd = true;
  120. return this;
  121. },
  122. // 对外接口:恢复到最初状态
  123. reverse: function() {
  124. if (!this.initprops) {
  125. alert('尚未调用任何动画,不能反转!');
  126. }
  127. this.animate(this.initprops);
  128. return this;
  129. },
  130. //
  131. runsequence: function(sequence) {
  132. let reSequence = sequence.reverse();
  133. reSequence.forEach((curItem, index) => {
  134. if (index >= ) {
  135. prevItem = reSequence[index - ];
  136. curItem.o.next = function() {
  137. var anim = Object.create(Animate);
  138. anim.init(prevItem.e);
  139. anim.animate(prevItem.p, prevItem.o);
  140. };
  141. }
  142. });
  143. var firstItem = reSequence[reSequence.length - ];
  144. var firstAnim = Object.create(Animate);
  145. firstAnim.init(firstItem.e);
  146. firstAnim.animate(firstItem.p, firstItem.o);
  147. },
  148. };

myanimation.js

预定义动画和预定义动画序列

  1. // 实现一些自定义动画
  2. ;
  3. (function(window) {
  4. const Animate = window.Animate;
  5. if (!Animate) {
  6. console.log('请首先引入myanimate.js');
  7. return;
  8. }
  9. const effects = {
  10. "transition.slideUpIn": {
  11. defaultDuration: ,
  12. calls: [
  13. [{ opacity: [, ], translateY: [, ] }]
  14. ]
  15. },
  16. "transition.slideUpOut": {
  17. defaultDuration: ,
  18. calls: [
  19. [{ opacity: [, ], translateY: - }]
  20. ],
  21. reset: { translateY: }
  22. },
  23. "transition.slideDownIn": {
  24. defaultDuration: ,
  25. calls: [
  26. [{ opacity: [, ], translateY: [, -] }]
  27. ]
  28. },
  29. "transition.slideDownOut": {
  30. defaultDuration: ,
  31. calls: [
  32. [{ opacity: [, ], translateY: }]
  33. ],
  34. reset: { translateY: }
  35. },
  36. "transition.slideLeftIn": {
  37. defaultDuration: ,
  38. calls: [
  39. [{ opacity: [, ], translateX: [, -] }]
  40. ]
  41. },
  42. "transition.slideLeftOut": {
  43. defaultDuration: ,
  44. calls: [
  45. [{ opacity: [, ], translateX: - }]
  46. ],
  47. reset: { translateX: }
  48. },
  49. "transition.slideRightIn": {
  50. defaultDuration: ,
  51. calls: [
  52. [{ opacity: [, ], translateX: [, ] }]
  53. ]
  54. },
  55. "transition.slideRightOut": {
  56. defaultDuration: ,
  57. calls: [
  58. [{ opacity: [, ], translateX: , translateZ: }]
  59. ],
  60. reset: { translateX: }
  61. },
  62. "callout.pulse": {
  63. defaultDuration: ,
  64. calls: [
  65. [{ scaleX: 1.1 }, 0.50],
  66. [{ scaleX: }, 0.50]
  67. ]
  68. },
  69. 'test': {
  70. defaultDuration: ,
  71. calls: [
  72. [{ left: , opacity: 0.1 }, 0.5],
  73. [{ opacity: }, 0.5]
  74. ]
  75. }
  76. };
  77. Animate.runEffect = function(effectName) {
  78. let curEffect = effects[effectName];
  79. if (!curEffect) {
  80. return;
  81. }
  82. let sequence = [];
  83. let defaultDuration = curEffect.defaultDuration;
  84. curEffect.calls.forEach((item, index) => {
  85. let propMap = item[];
  86. let duration = item[] ? item[] * defaultDuration : defaultDuration;
  87. let options = item[] || {};
  88. options.duration = duration;
  89. sequence.push({
  90. e: this.dom,
  91. p: propMap,
  92. o: options
  93. });
  94. });
  95. Animate.runsequence(sequence);
  96. };
  97.  
  98. })(window);

myanimation.effect.js

测试代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>测试动画库</title>
  7. <style type="text/css">
  8. .main {
  9. padding: 50px;
  10. position: relative;
  11. }
  12.  
  13. .btn-wrapper {
  14. padding: 15px ;
  15. }
  16.  
  17. .mydiv {
  18. margin: 20px ;
  19. width: 300px;
  20. height: 200px;
  21. background-color: pink;
  22. position: relative;
  23. top: ;
  24. left: ;
  25. }
  26. </style>
  27. </head>
  28.  
  29. <body>
  30. <div class="main">
  31. <div class="mydiv" id="mydiv"></div>
  32. <div id="btn-wrapper">
  33. <button id="chainBtn">链式调用</button>
  34. </div>
  35. <div class="mydiv" id="mydiv-reverse"></div>
  36. <div id="btn-wrapper">
  37. <button id="reverseBtn">reverse调用</button>
  38. </div>
  39. <div class="mydiv" id="mydiv-predefine1"></div>
  40. <div class="mydiv" id="mydiv-predefine2"></div>
  41. <div id="btn-wrapper">
  42. <button id="predefineBtn">预定义动画队列</button>
  43. </div>
  44. <div class="mydiv" id="mydiv-effect"></div>
  45. <div id="btn-wrapper">
  46. <button id="effectBtn">预定义动画</button>
  47. </div>
  48. </div>
  49. <script src="./util.js"></script>
  50. <script src="./tween.js"></script>
  51. <script src="./myanimate.js"></script>
  52. <script src="./myanimate.effect.js"></script>
  53. <script type="text/javascript">
  54. // 链式调用
  55. document.querySelector('#chainBtn').addEventListener('click', function(e) {
  56. var div = document.getElementById('mydiv');
  57. var anim = Object.create(Animate);
  58. anim.init(div);
  59. anim.animate({
  60. opacity: 0.2
  61. }).animate({
  62. left:
  63. });
  64. //测试停止动画,stop函数
  65. // setTimeout(function() {
  66. // anim.stop();
  67. // }, 500);
  68. //测试直接到达动画的最终状态,finish函数
  69. //如果是链式调用,到达所有动画的最终状态
  70. //如果只想到达当前动画的最终状态,只需要稍微修改,在stop中重置toEnd=false即可
  71. // setTimeout(function() {
  72. // anim.finish();
  73. // }, 500);
  74. });
  75.  
  76. //reverse调用
  77. document.querySelector('#reverseBtn').addEventListener('click', function(e) {
  78. var div = document.getElementById('mydiv-reverse');
  79. var anim = Object.create(Animate);
  80. anim.init(div);
  81. anim.animate({
  82. left:
  83. }).reverse();
  84. });
  85.  
  86. //预定义动画测试
  87. document.querySelector('#predefineBtn').addEventListener('click', function(e) {
  88. var anims = [{
  89. e: '#mydiv-predefine1',
  90. p: {
  91. left:
  92. },
  93. o: {
  94. duration:
  95. }
  96. }, {
  97. e: '#mydiv-predefine2',
  98. p: {
  99. left: ,
  100. opacity: 0.3
  101. },
  102. o: {
  103. duration:
  104. }
  105. }];
  106. //不需要新建一个实例,直接在Animate上调用即可
  107. Animate.runsequence(anims);
  108. });
  109. //预定义动画测试
  110. document.querySelector('#effectBtn').addEventListener('click', function(e) {
  111. var anim = Object.create(Animate);
  112. anim.init('#mydiv-effect');
  113. anim.runEffect('test');
  114. });
  115. </script>
  116. </body>
  117.  
  118. </html>

index.html

附上一份jQuery动画部分的源代码

  1. var fxNow, timerId,
  2. rfxtypes = /^(?:toggle|show|hide)$/,
  3. rfxnum = new RegExp( "^(?:([+-])=|)(" + core_pnum + ")([a-z%]*)$", "i" ),
  4. rrun = /queueHooks$/,
  5. animationPrefilters = [ defaultPrefilter ],
  6. tweeners = {
  7. "*": [function( prop, value ) {
  8. var tween = this.createTween( prop, value ),
  9. target = tween.cur(),
  10. parts = rfxnum.exec( value ),
  11. unit = parts && parts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
  12.  
  13. // Starting value computation is required for potential unit mismatches
  14. start = ( jQuery.cssNumber[ prop ] || unit !== "px" && +target ) &&
  15. rfxnum.exec( jQuery.css( tween.elem, prop ) ),
  16. scale = 1,
  17. maxIterations = 20;
  18.  
  19. if ( start && start[ 3 ] !== unit ) {
  20. // Trust units reported by jQuery.css
  21. unit = unit || start[ 3 ];
  22.  
  23. // Make sure we update the tween properties later on
  24. parts = parts || [];
  25.  
  26. // Iteratively approximate from a nonzero starting point
  27. start = +target || 1;
  28.  
  29. do {
  30. // If previous iteration zeroed out, double until we get *something*
  31. // Use a string for doubling factor so we don't accidentally see scale as unchanged below
  32. scale = scale || ".5";
  33.  
  34. // Adjust and apply
  35. start = start / scale;
  36. jQuery.style( tween.elem, prop, start + unit );
  37.  
  38. // Update scale, tolerating zero or NaN from tween.cur()
  39. // And breaking the loop if scale is unchanged or perfect, or if we've just had enough
  40. } while ( scale !== (scale = tween.cur() / target) && scale !== 1 && --maxIterations );
  41. }
  42.  
  43. // Update tween properties
  44. if ( parts ) {
  45. start = tween.start = +start || +target || 0;
  46. tween.unit = unit;
  47. // If a +=/-= token was provided, we're doing a relative animation
  48. tween.end = parts[ 1 ] ?
  49. start + ( parts[ 1 ] + 1 ) * parts[ 2 ] :
  50. +parts[ 2 ];
  51. }
  52.  
  53. return tween;
  54. }]
  55. };
  56.  
  57. // Animations created synchronously will run synchronously
  58. function createFxNow() {
  59. setTimeout(function() {
  60. fxNow = undefined;
  61. });
  62. return ( fxNow = jQuery.now() );
  63. }
  64.  
  65. function createTween( value, prop, animation ) {
  66. var tween,
  67. collection = ( tweeners[ prop ] || [] ).concat( tweeners[ "*" ] ),
  68. index = 0,
  69. length = collection.length;
  70. for ( ; index < length; index++ ) {
  71. if ( (tween = collection[ index ].call( animation, prop, value )) ) {
  72.  
  73. // we're done with this property
  74. return tween;
  75. }
  76. }
  77. }
  78.  
  79. function Animation( elem, properties, options ) {
  80. var result,
  81. stopped,
  82. index = 0,
  83. length = animationPrefilters.length,
  84. deferred = jQuery.Deferred().always( function() {
  85. // don't match elem in the :animated selector
  86. delete tick.elem;
  87. }),
  88. tick = function() {
  89. if ( stopped ) {
  90. return false;
  91. }
  92. var currentTime = fxNow || createFxNow(),
  93. remaining = Math.max( 0, animation.startTime + animation.duration - currentTime ),
  94. // archaic crash bug won't allow us to use 1 - ( 0.5 || 0 ) (#12497)
  95. temp = remaining / animation.duration || 0,
  96. percent = 1 - temp,
  97. index = 0,
  98. length = animation.tweens.length;
  99.  
  100. for ( ; index < length ; index++ ) {
  101. animation.tweens[ index ].run( percent );
  102. }
  103.  
  104. deferred.notifyWith( elem, [ animation, percent, remaining ]);
  105.  
  106. if ( percent < 1 && length ) {
  107. return remaining;
  108. } else {
  109. deferred.resolveWith( elem, [ animation ] );
  110. return false;
  111. }
  112. },
  113. animation = deferred.promise({
  114. elem: elem,
  115. props: jQuery.extend( {}, properties ),
  116. opts: jQuery.extend( true, { specialEasing: {} }, options ),
  117. originalProperties: properties,
  118. originalOptions: options,
  119. startTime: fxNow || createFxNow(),
  120. duration: options.duration,
  121. tweens: [],
  122. createTween: function( prop, end ) {
  123. var tween = jQuery.Tween( elem, animation.opts, prop, end,
  124. animation.opts.specialEasing[ prop ] || animation.opts.easing );
  125. animation.tweens.push( tween );
  126. return tween;
  127. },
  128. stop: function( gotoEnd ) {
  129. var index = 0,
  130. // if we are going to the end, we want to run all the tweens
  131. // otherwise we skip this part
  132. length = gotoEnd ? animation.tweens.length : 0;
  133. if ( stopped ) {
  134. return this;
  135. }
  136. stopped = true;
  137. for ( ; index < length ; index++ ) {
  138. animation.tweens[ index ].run( 1 );
  139. }
  140.  
  141. // resolve when we played the last frame
  142. // otherwise, reject
  143. if ( gotoEnd ) {
  144. deferred.resolveWith( elem, [ animation, gotoEnd ] );
  145. } else {
  146. deferred.rejectWith( elem, [ animation, gotoEnd ] );
  147. }
  148. return this;
  149. }
  150. }),
  151. props = animation.props;
  152.  
  153. propFilter( props, animation.opts.specialEasing );
  154.  
  155. for ( ; index < length ; index++ ) {
  156. result = animationPrefilters[ index ].call( animation, elem, props, animation.opts );
  157. if ( result ) {
  158. return result;
  159. }
  160. }
  161.  
  162. jQuery.map( props, createTween, animation );
  163.  
  164. if ( jQuery.isFunction( animation.opts.start ) ) {
  165. animation.opts.start.call( elem, animation );
  166. }
  167.  
  168. jQuery.fx.timer(
  169. jQuery.extend( tick, {
  170. elem: elem,
  171. anim: animation,
  172. queue: animation.opts.queue
  173. })
  174. );
  175.  
  176. // attach callbacks from options
  177. return animation.progress( animation.opts.progress )
  178. .done( animation.opts.done, animation.opts.complete )
  179. .fail( animation.opts.fail )
  180. .always( animation.opts.always );
  181. }
  182.  
  183. function propFilter( props, specialEasing ) {
  184. var index, name, easing, value, hooks;
  185.  
  186. // camelCase, specialEasing and expand cssHook pass
  187. for ( index in props ) {
  188. name = jQuery.camelCase( index );
  189. easing = specialEasing[ name ];
  190. value = props[ index ];
  191. if ( jQuery.isArray( value ) ) {
  192. easing = value[ 1 ];
  193. value = props[ index ] = value[ 0 ];
  194. }
  195.  
  196. if ( index !== name ) {
  197. props[ name ] = value;
  198. delete props[ index ];
  199. }
  200.  
  201. hooks = jQuery.cssHooks[ name ];
  202. if ( hooks && "expand" in hooks ) {
  203. value = hooks.expand( value );
  204. delete props[ name ];
  205.  
  206. // not quite $.extend, this wont overwrite keys already present.
  207. // also - reusing 'index' from above because we have the correct "name"
  208. for ( index in value ) {
  209. if ( !( index in props ) ) {
  210. props[ index ] = value[ index ];
  211. specialEasing[ index ] = easing;
  212. }
  213. }
  214. } else {
  215. specialEasing[ name ] = easing;
  216. }
  217. }
  218. }
  219.  
  220. jQuery.Animation = jQuery.extend( Animation, {
  221.  
  222. tweener: function( props, callback ) {
  223. if ( jQuery.isFunction( props ) ) {
  224. callback = props;
  225. props = [ "*" ];
  226. } else {
  227. props = props.split(" ");
  228. }
  229.  
  230. var prop,
  231. index = 0,
  232. length = props.length;
  233.  
  234. for ( ; index < length ; index++ ) {
  235. prop = props[ index ];
  236. tweeners[ prop ] = tweeners[ prop ] || [];
  237. tweeners[ prop ].unshift( callback );
  238. }
  239. },
  240.  
  241. prefilter: function( callback, prepend ) {
  242. if ( prepend ) {
  243. animationPrefilters.unshift( callback );
  244. } else {
  245. animationPrefilters.push( callback );
  246. }
  247. }
  248. });
  249.  
  250. function defaultPrefilter( elem, props, opts ) {
  251. /* jshint validthis: true */
  252. var prop, value, toggle, tween, hooks, oldfire,
  253. anim = this,
  254. orig = {},
  255. style = elem.style,
  256. hidden = elem.nodeType && isHidden( elem ),
  257. dataShow = data_priv.get( elem, "fxshow" );
  258.  
  259. // handle queue: false promises
  260. if ( !opts.queue ) {
  261. hooks = jQuery._queueHooks( elem, "fx" );
  262. if ( hooks.unqueued == null ) {
  263. hooks.unqueued = 0;
  264. oldfire = hooks.empty.fire;
  265. hooks.empty.fire = function() {
  266. if ( !hooks.unqueued ) {
  267. oldfire();
  268. }
  269. };
  270. }
  271. hooks.unqueued++;
  272.  
  273. anim.always(function() {
  274. // doing this makes sure that the complete handler will be called
  275. // before this completes
  276. anim.always(function() {
  277. hooks.unqueued--;
  278. if ( !jQuery.queue( elem, "fx" ).length ) {
  279. hooks.empty.fire();
  280. }
  281. });
  282. });
  283. }
  284.  
  285. // height/width overflow pass
  286. if ( elem.nodeType === 1 && ( "height" in props || "width" in props ) ) {
  287. // Make sure that nothing sneaks out
  288. // Record all 3 overflow attributes because IE9-10 do not
  289. // change the overflow attribute when overflowX and
  290. // overflowY are set to the same value
  291. opts.overflow = [ style.overflow, style.overflowX, style.overflowY ];
  292.  
  293. // Set display property to inline-block for height/width
  294. // animations on inline elements that are having width/height animated
  295. if ( jQuery.css( elem, "display" ) === "inline" &&
  296. jQuery.css( elem, "float" ) === "none" ) {
  297.  
  298. style.display = "inline-block";
  299. }
  300. }
  301.  
  302. if ( opts.overflow ) {
  303. style.overflow = "hidden";
  304. anim.always(function() {
  305. style.overflow = opts.overflow[ 0 ];
  306. style.overflowX = opts.overflow[ 1 ];
  307. style.overflowY = opts.overflow[ 2 ];
  308. });
  309. }
  310.  
  311. // show/hide pass
  312. for ( prop in props ) {
  313. value = props[ prop ];
  314. if ( rfxtypes.exec( value ) ) {
  315. delete props[ prop ];
  316. toggle = toggle || value === "toggle";
  317. if ( value === ( hidden ? "hide" : "show" ) ) {
  318.  
  319. // If there is dataShow left over from a stopped hide or show and we are going to proceed with show, we should pretend to be hidden
  320. if ( value === "show" && dataShow && dataShow[ prop ] !== undefined ) {
  321. hidden = true;
  322. } else {
  323. continue;
  324. }
  325. }
  326. orig[ prop ] = dataShow && dataShow[ prop ] || jQuery.style( elem, prop );
  327. }
  328. }
  329.  
  330. if ( !jQuery.isEmptyObject( orig ) ) {
  331. if ( dataShow ) {
  332. if ( "hidden" in dataShow ) {
  333. hidden = dataShow.hidden;
  334. }
  335. } else {
  336. dataShow = data_priv.access( elem, "fxshow", {} );
  337. }
  338.  
  339. // store state if its toggle - enables .stop().toggle() to "reverse"
  340. if ( toggle ) {
  341. dataShow.hidden = !hidden;
  342. }
  343. if ( hidden ) {
  344. jQuery( elem ).show();
  345. } else {
  346. anim.done(function() {
  347. jQuery( elem ).hide();
  348. });
  349. }
  350. anim.done(function() {
  351. var prop;
  352.  
  353. data_priv.remove( elem, "fxshow" );
  354. for ( prop in orig ) {
  355. jQuery.style( elem, prop, orig[ prop ] );
  356. }
  357. });
  358. for ( prop in orig ) {
  359. tween = createTween( hidden ? dataShow[ prop ] : 0, prop, anim );
  360.  
  361. if ( !( prop in dataShow ) ) {
  362. dataShow[ prop ] = tween.start;
  363. if ( hidden ) {
  364. tween.end = tween.start;
  365. tween.start = prop === "width" || prop === "height" ? 1 : 0;
  366. }
  367. }
  368. }
  369. }
  370. }
  371.  
  372. function Tween( elem, options, prop, end, easing ) {
  373. return new Tween.prototype.init( elem, options, prop, end, easing );
  374. }
  375. jQuery.Tween = Tween;
  376.  
  377. Tween.prototype = {
  378. constructor: Tween,
  379. init: function( elem, options, prop, end, easing, unit ) {
  380. this.elem = elem;
  381. this.prop = prop;
  382. this.easing = easing || "swing";
  383. this.options = options;
  384. this.start = this.now = this.cur();
  385. this.end = end;
  386. this.unit = unit || ( jQuery.cssNumber[ prop ] ? "" : "px" );
  387. },
  388. cur: function() {
  389. var hooks = Tween.propHooks[ this.prop ];
  390.  
  391. return hooks && hooks.get ?
  392. hooks.get( this ) :
  393. Tween.propHooks._default.get( this );
  394. },
  395. run: function( percent ) {
  396. var eased,
  397. hooks = Tween.propHooks[ this.prop ];
  398.  
  399. if ( this.options.duration ) {
  400. this.pos = eased = jQuery.easing[ this.easing ](
  401. percent, this.options.duration * percent, 0, 1, this.options.duration
  402. );
  403. } else {
  404. this.pos = eased = percent;
  405. }
  406. this.now = ( this.end - this.start ) * eased + this.start;
  407.  
  408. if ( this.options.step ) {
  409. this.options.step.call( this.elem, this.now, this );
  410. }
  411.  
  412. if ( hooks && hooks.set ) {
  413. hooks.set( this );
  414. } else {
  415. Tween.propHooks._default.set( this );
  416. }
  417. return this;
  418. }
  419. };
  420.  
  421. Tween.prototype.init.prototype = Tween.prototype;
  422.  
  423. Tween.propHooks = {
  424. _default: {
  425. get: function( tween ) {
  426. var result;
  427.  
  428. if ( tween.elem[ tween.prop ] != null &&
  429. (!tween.elem.style || tween.elem.style[ tween.prop ] == null) ) {
  430. return tween.elem[ tween.prop ];
  431. }
  432.  
  433. // passing an empty string as a 3rd parameter to .css will automatically
  434. // attempt a parseFloat and fallback to a string if the parse fails
  435. // so, simple values such as "10px" are parsed to Float.
  436. // complex values such as "rotate(1rad)" are returned as is.
  437. result = jQuery.css( tween.elem, tween.prop, "" );
  438. // Empty strings, null, undefined and "auto" are converted to 0.
  439. return !result || result === "auto" ? 0 : result;
  440. },
  441. set: function( tween ) {
  442. // use step hook for back compat - use cssHook if its there - use .style if its
  443. // available and use plain properties where available
  444. if ( jQuery.fx.step[ tween.prop ] ) {
  445. jQuery.fx.step[ tween.prop ]( tween );
  446. } else if ( tween.elem.style && ( tween.elem.style[ jQuery.cssProps[ tween.prop ] ] != null || jQuery.cssHooks[ tween.prop ] ) ) {
  447. jQuery.style( tween.elem, tween.prop, tween.now + tween.unit );
  448. } else {
  449. tween.elem[ tween.prop ] = tween.now;
  450. }
  451. }
  452. }
  453. };
  454.  
  455. // Support: IE9
  456. // Panic based approach to setting things on disconnected nodes
  457.  
  458. Tween.propHooks.scrollTop = Tween.propHooks.scrollLeft = {
  459. set: function( tween ) {
  460. if ( tween.elem.nodeType && tween.elem.parentNode ) {
  461. tween.elem[ tween.prop ] = tween.now;
  462. }
  463. }
  464. };
  465.  
  466. jQuery.each([ "toggle", "show", "hide" ], function( i, name ) {
  467. var cssFn = jQuery.fn[ name ];
  468. jQuery.fn[ name ] = function( speed, easing, callback ) {
  469. return speed == null || typeof speed === "boolean" ?
  470. cssFn.apply( this, arguments ) :
  471. this.animate( genFx( name, true ), speed, easing, callback );
  472. };
  473. });
  474.  
  475. jQuery.fn.extend({
  476. fadeTo: function( speed, to, easing, callback ) {
  477.  
  478. // show any hidden elements after setting opacity to 0
  479. return this.filter( isHidden ).css( "opacity", 0 ).show()
  480.  
  481. // animate to the value specified
  482. .end().animate({ opacity: to }, speed, easing, callback );
  483. },
  484. animate: function( prop, speed, easing, callback ) {
  485. var empty = jQuery.isEmptyObject( prop ),
  486. optall = jQuery.speed( speed, easing, callback ),
  487. doAnimation = function() {
  488. // Operate on a copy of prop so per-property easing won't be lost
  489. var anim = Animation( this, jQuery.extend( {}, prop ), optall );
  490.  
  491. // Empty animations, or finishing resolves immediately
  492. if ( empty || data_priv.get( this, "finish" ) ) {
  493. anim.stop( true );
  494. }
  495. };
  496. doAnimation.finish = doAnimation;
  497.  
  498. return empty || optall.queue === false ?
  499. this.each( doAnimation ) :
  500. this.queue( optall.queue, doAnimation );
  501. },
  502. stop: function( type, clearQueue, gotoEnd ) {
  503. var stopQueue = function( hooks ) {
  504. var stop = hooks.stop;
  505. delete hooks.stop;
  506. stop( gotoEnd );
  507. };
  508.  
  509. if ( typeof type !== "string" ) {
  510. gotoEnd = clearQueue;
  511. clearQueue = type;
  512. type = undefined;
  513. }
  514. if ( clearQueue && type !== false ) {
  515. this.queue( type || "fx", [] );
  516. }
  517.  
  518. return this.each(function() {
  519. var dequeue = true,
  520. index = type != null && type + "queueHooks",
  521. timers = jQuery.timers,
  522. data = data_priv.get( this );
  523.  
  524. if ( index ) {
  525. if ( data[ index ] && data[ index ].stop ) {
  526. stopQueue( data[ index ] );
  527. }
  528. } else {
  529. for ( index in data ) {
  530. if ( data[ index ] && data[ index ].stop && rrun.test( index ) ) {
  531. stopQueue( data[ index ] );
  532. }
  533. }
  534. }
  535.  
  536. for ( index = timers.length; index--; ) {
  537. if ( timers[ index ].elem === this && (type == null || timers[ index ].queue === type) ) {
  538. timers[ index ].anim.stop( gotoEnd );
  539. dequeue = false;
  540. timers.splice( index, 1 );
  541. }
  542. }
  543.  
  544. // start the next in the queue if the last step wasn't forced
  545. // timers currently will call their complete callbacks, which will dequeue
  546. // but only if they were gotoEnd
  547. if ( dequeue || !gotoEnd ) {
  548. jQuery.dequeue( this, type );
  549. }
  550. });
  551. },
  552. finish: function( type ) {
  553. if ( type !== false ) {
  554. type = type || "fx";
  555. }
  556. return this.each(function() {
  557. var index,
  558. data = data_priv.get( this ),
  559. queue = data[ type + "queue" ],
  560. hooks = data[ type + "queueHooks" ],
  561. timers = jQuery.timers,
  562. length = queue ? queue.length : 0;
  563.  
  564. // enable finishing flag on private data
  565. data.finish = true;
  566.  
  567. // empty the queue first
  568. jQuery.queue( this, type, [] );
  569.  
  570. if ( hooks && hooks.stop ) {
  571. hooks.stop.call( this, true );
  572. }
  573.  
  574. // look for any active animations, and finish them
  575. for ( index = timers.length; index--; ) {
  576. if ( timers[ index ].elem === this && timers[ index ].queue === type ) {
  577. timers[ index ].anim.stop( true );
  578. timers.splice( index, 1 );
  579. }
  580. }
  581.  
  582. // look for any animations in the old queue and finish them
  583. for ( index = 0; index < length; index++ ) {
  584. if ( queue[ index ] && queue[ index ].finish ) {
  585. queue[ index ].finish.call( this );
  586. }
  587. }
  588.  
  589. // turn off finishing flag
  590. delete data.finish;
  591. });
  592. }
  593. });
  594.  
  595. // Generate parameters to create a standard animation
  596. function genFx( type, includeWidth ) {
  597. var which,
  598. attrs = { height: type },
  599. i = 0;
  600.  
  601. // if we include width, step value is 1 to do all cssExpand values,
  602. // if we don't include width, step value is 2 to skip over Left and Right
  603. includeWidth = includeWidth? 1 : 0;
  604. for( ; i < 4 ; i += 2 - includeWidth ) {
  605. which = cssExpand[ i ];
  606. attrs[ "margin" + which ] = attrs[ "padding" + which ] = type;
  607. }
  608.  
  609. if ( includeWidth ) {
  610. attrs.opacity = attrs.width = type;
  611. }
  612.  
  613. return attrs;
  614. }
  615.  
  616. // Generate shortcuts for custom animations
  617. jQuery.each({
  618. slideDown: genFx("show"),
  619. slideUp: genFx("hide"),
  620. slideToggle: genFx("toggle"),
  621. fadeIn: { opacity: "show" },
  622. fadeOut: { opacity: "hide" },
  623. fadeToggle: { opacity: "toggle" }
  624. }, function( name, props ) {
  625. jQuery.fn[ name ] = function( speed, easing, callback ) {
  626. return this.animate( props, speed, easing, callback );
  627. };
  628. });
  629.  
  630. jQuery.speed = function( speed, easing, fn ) {
  631. var opt = speed && typeof speed === "object" ? jQuery.extend( {}, speed ) : {
  632. complete: fn || !fn && easing ||
  633. jQuery.isFunction( speed ) && speed,
  634. duration: speed,
  635. easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
  636. };
  637.  
  638. opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
  639. opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
  640.  
  641. // normalize opt.queue - true/undefined/null -> "fx"
  642. if ( opt.queue == null || opt.queue === true ) {
  643. opt.queue = "fx";
  644. }
  645.  
  646. // Queueing
  647. opt.old = opt.complete;
  648.  
  649. opt.complete = function() {
  650. if ( jQuery.isFunction( opt.old ) ) {
  651. opt.old.call( this );
  652. }
  653.  
  654. if ( opt.queue ) {
  655. jQuery.dequeue( this, opt.queue );
  656. }
  657. };
  658.  
  659. return opt;
  660. };
  661.  
  662. jQuery.easing = {
  663. linear: function( p ) {
  664. return p;
  665. },
  666. swing: function( p ) {
  667. return 0.5 - Math.cos( p*Math.PI ) / 2;
  668. }
  669. };
  670.  
  671. jQuery.timers = [];
  672. jQuery.fx = Tween.prototype.init;
  673. jQuery.fx.tick = function() {
  674. var timer,
  675. timers = jQuery.timers,
  676. i = 0;
  677.  
  678. fxNow = jQuery.now();
  679.  
  680. for ( ; i < timers.length; i++ ) {
  681. timer = timers[ i ];
  682. // Checks the timer has not already been removed
  683. if ( !timer() && timers[ i ] === timer ) {
  684. timers.splice( i--, 1 );
  685. }
  686. }
  687.  
  688. if ( !timers.length ) {
  689. jQuery.fx.stop();
  690. }
  691. fxNow = undefined;
  692. };
  693.  
  694. jQuery.fx.timer = function( timer ) {
  695. if ( timer() && jQuery.timers.push( timer ) ) {
  696. jQuery.fx.start();
  697. }
  698. };
  699.  
  700. jQuery.fx.interval = 13;
  701.  
  702. jQuery.fx.start = function() {
  703. if ( !timerId ) {
  704. timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
  705. }
  706. };
  707.  
  708. jQuery.fx.stop = function() {
  709. clearInterval( timerId );
  710. timerId = null;
  711. };
  712.  
  713. jQuery.fx.speeds = {
  714. slow: 600,
  715. fast: 200,
  716. // Default speed
  717. _default: 400
  718. };
  719.  
  720. // Back Compat <1.8 extension point
  721. jQuery.fx.step = {};
  722.  
  723. if ( jQuery.expr && jQuery.expr.filters ) {
  724. jQuery.expr.filters.animated = function( elem ) {
  725. return jQuery.grep(jQuery.timers, function( fn ) {
  726. return elem === fn.elem;
  727. }).length;
  728. };
  729. }
  730. jQuery.fn.offset = function( options ) {
  731. if ( arguments.length ) {
  732. return options === undefined ?
  733. this :
  734. this.each(function( i ) {
  735. jQuery.offset.setOffset( this, options, i );
  736. });
  737. }
  738.  
  739. var docElem, win,
  740. elem = this[ 0 ],
  741. box = { top: 0, left: 0 },
  742. doc = elem && elem.ownerDocument;
  743.  
  744. if ( !doc ) {
  745. return;
  746. }
  747.  
  748. docElem = doc.documentElement;
  749.  
  750. // Make sure it's not a disconnected DOM node
  751. if ( !jQuery.contains( docElem, elem ) ) {
  752. return box;
  753. }
  754.  
  755. // If we don't have gBCR, just use 0,0 rather than error
  756. // BlackBerry 5, iOS 3 (original iPhone)
  757. if ( typeof elem.getBoundingClientRect !== core_strundefined ) {
  758. box = elem.getBoundingClientRect();
  759. }
  760. win = getWindow( doc );
  761. return {
  762. top: box.top + win.pageYOffset - docElem.clientTop,
  763. left: box.left + win.pageXOffset - docElem.clientLeft
  764. };
  765. };
  766.  
  767. jQuery.offset = {
  768.  
  769. setOffset: function( elem, options, i ) {
  770. var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
  771. position = jQuery.css( elem, "position" ),
  772. curElem = jQuery( elem ),
  773. props = {};
  774.  
  775. // Set position first, in-case top/left are set even on static elem
  776. if ( position === "static" ) {
  777. elem.style.position = "relative";
  778. }
  779.  
  780. curOffset = curElem.offset();
  781. curCSSTop = jQuery.css( elem, "top" );
  782. curCSSLeft = jQuery.css( elem, "left" );
  783. calculatePosition = ( position === "absolute" || position === "fixed" ) && ( curCSSTop + curCSSLeft ).indexOf("auto") > -1;
  784.  
  785. // Need to be able to calculate position if either top or left is auto and position is either absolute or fixed
  786. if ( calculatePosition ) {
  787. curPosition = curElem.position();
  788. curTop = curPosition.top;
  789. curLeft = curPosition.left;
  790.  
  791. } else {
  792. curTop = parseFloat( curCSSTop ) || 0;
  793. curLeft = parseFloat( curCSSLeft ) || 0;
  794. }
  795.  
  796. if ( jQuery.isFunction( options ) ) {
  797. options = options.call( elem, i, curOffset );
  798. }
  799.  
  800. if ( options.top != null ) {
  801. props.top = ( options.top - curOffset.top ) + curTop;
  802. }
  803. if ( options.left != null ) {
  804. props.left = ( options.left - curOffset.left ) + curLeft;
  805. }
  806.  
  807. if ( "using" in options ) {
  808. options.using.call( elem, props );
  809.  
  810. } else {
  811. curElem.css( props );
  812. }
  813. }
  814. };
  815.  
  816. jQuery.fn.extend({
  817.  
  818. position: function() {
  819. if ( !this[ 0 ] ) {
  820. return;
  821. }
  822.  
  823. var offsetParent, offset,
  824. elem = this[ 0 ],
  825. parentOffset = { top: 0, left: 0 };
  826.  
  827. // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is it's only offset parent
  828. if ( jQuery.css( elem, "position" ) === "fixed" ) {
  829. // We assume that getBoundingClientRect is available when computed position is fixed
  830. offset = elem.getBoundingClientRect();
  831.  
  832. } else {
  833. // Get *real* offsetParent
  834. offsetParent = this.offsetParent();
  835.  
  836. // Get correct offsets
  837. offset = this.offset();
  838. if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
  839. parentOffset = offsetParent.offset();
  840. }
  841.  
  842. // Add offsetParent borders
  843. parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
  844. parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
  845. }
  846.  
  847. // Subtract parent offsets and element margins
  848. return {
  849. top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
  850. left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
  851. };
  852. },
  853.  
  854. offsetParent: function() {
  855. return this.map(function() {
  856. var offsetParent = this.offsetParent || docElem;
  857.  
  858. while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position") === "static" ) ) {
  859. offsetParent = offsetParent.offsetParent;
  860. }
  861.  
  862. return offsetParent || docElem;
  863. });
  864. }
  865. });
  866.  
  867. // Create scrollLeft and scrollTop methods
  868. jQuery.each( {scrollLeft: "pageXOffset", scrollTop: "pageYOffset"}, function( method, prop ) {
  869. var top = "pageYOffset" === prop;
  870.  
  871. jQuery.fn[ method ] = function( val ) {
  872. return jQuery.access( this, function( elem, method, val ) {
  873. var win = getWindow( elem );
  874.  
  875. if ( val === undefined ) {
  876. return win ? win[ prop ] : elem[ method ];
  877. }
  878.  
  879. if ( win ) {
  880. win.scrollTo(
  881. !top ? val : window.pageXOffset,
  882. top ? val : window.pageYOffset
  883. );
  884.  
  885. } else {
  886. elem[ method ] = val;
  887. }
  888. }, method, val, arguments.length, null );
  889. };
  890. });
  891.  
  892. function getWindow( elem ) {
  893. return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView;
  894. }
  895. // Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
  896. jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
  897. jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name }, function( defaultExtra, funcName ) {
  898. // margin is only for outerHeight, outerWidth
  899. jQuery.fn[ funcName ] = function( margin, value ) {
  900. var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
  901. extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );
  902.  
  903. return jQuery.access( this, function( elem, type, value ) {
  904. var doc;
  905.  
  906. if ( jQuery.isWindow( elem ) ) {
  907. // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
  908. // isn't a whole lot we can do. See pull request at this URL for discussion:
  909. // https://github.com/jquery/jquery/pull/764
  910. return elem.document.documentElement[ "client" + name ];
  911. }
  912.  
  913. // Get document width or height
  914. if ( elem.nodeType === 9 ) {
  915. doc = elem.documentElement;
  916.  
  917. // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height],
  918. // whichever is greatest
  919. return Math.max(
  920. elem.body[ "scroll" + name ], doc[ "scroll" + name ],
  921. elem.body[ "offset" + name ], doc[ "offset" + name ],
  922. doc[ "client" + name ]
  923. );
  924. }
  925.  
  926. return value === undefined ?
  927. // Get width or height on the element, requesting but not forcing parseFloat
  928. jQuery.css( elem, type, extra ) :
  929.  
  930. // Set width or height on the element
  931. jQuery.style( elem, type, value, extra );
  932. }, type, chainable ? margin : undefined, chainable, null );
  933. };
  934. });
  935. });

jQuery动画部分源码

原生javascript封装动画库的更多相关文章

  1. 原生JavaScript 封装ajax

    原生JavaScript 封装ajax   function myajax(options){ //新建一个局部对象 用来存放用户输入的各种参数 var opt={ type:options.type ...

  2. 原生JavaScript封装的jsonp跨域请求

    原生JavaScript封装的jsonp跨域请求 <!DOCTYPE html> <html lang="en"> <head> <met ...

  3. 原生javascript封装ajax和jsonp

    在我们请求数据时,完成页面跨域,利用原生JS封装的ajax和jsonp: <!DOCTYPE html> <html lang="en"> <head ...

  4. 原生javascript封装的函数

    1.javascript 加载的函数 window.onload = function(){} 2.封装的id函数 function $(id) { return document.getElemen ...

  5. 原生JavaScript封装Ajax

    第一次开个人技术博客了,发的第一篇技术文章,欢迎指点…… 欢迎访问本人的独立博客:蓝克比尔 Ajax的实现主要分为四部分: 1.创建Ajax对象 // 创建ajax对象 var xhr = null; ...

  6. 2019年10个最受欢迎的JavaScript动画库!

    摘要: 非常炫酷的动画库! 原文:值得看看,2019 年 11 个受欢迎的 JavaScript 动画库! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 1. Three.js 超过 ...

  7. Snabbt.js – 极简的 JavaScript 动画库

    Snabbt.js 是一个简约的 JavaScript 动画库.它会平移,旋转,缩放,倾斜和调整你的元素.通过矩阵乘法运算,变换等可以任何你想要的方式进行组合.最终的结果通过 CSS3 变换矩阵设置. ...

  8. 10个最好的 JavaScript 动画库和开发框架

    虽然 CSS3 动画功能能够让我们以简单轻松的方式实现动画效果,但是浏览器兼容性问题让人头疼.不过不用担心,我们还有另外的武器——JavaScript,它同样可以帮助你实现各种各样的动画效果,而且借助 ...

  9. JavaScript 动画库和开发框架

    1. Tween JS TweenJS 是一个简单的 JavaScript 补间动画库.能够很好的和 EaselJS 库集成,但也不依赖或特定于它.它支持渐变的数字对象属性和 CSS 样式属性.API ...

随机推荐

  1. C# 批量设置窗体中控件状态的方法

    在开发中常遇到当点击某个按钮的时候,禁用文本框或按钮的的状态,以防止误操作,下面的代码是我已批量设置指定控件中的按钮状态的代码,同理可以延伸出很多操作. /// <summary> /// ...

  2. Maven错误:警告Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published

    该错误是在我将一个普通的由maven管理的java项目变为javaweb项目后出现的,由警告可以看出是说maven的类路径容器不会被导出或发布(即通过maven管理的依赖不会被导出或发布),那么我们用 ...

  3. bootstrap-table的简单使用

    先上效果图: 第一步:引用bootstrap-table的样式和js. @Styles.Render("~/assets/css/bootstrap.css") @Styles.R ...

  4. CUDA中使用多维数组

    今天想起一个问题,看到的绝大多数CUDA代码都是使用的一维数组,是否可以在CUDA中使用一维数组,这是一个问题,想了各种问题,各种被77的错误状态码和段错误折磨,最后发现有一个cudaMallocMa ...

  5. 算法---Face_Recognition配置实战篇

    python人脸识别库Face_Recognition-实操篇 @WP20190307 ================================目 录===================== ...

  6. Objective-C语法总结收集

    PART1--详解Objective-C语法快速参考 一.XCode.Objective-C.Cocoa说的是几样东西? 答案:三样东西. XCode:你可以把它看成是一个开发环境,就好像Visual ...

  7. VMware厚置备延迟置零,厚置备置零,精简置备详解

    1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦除物理设备上保留的任何数据,但是以后从虚拟机首次执行写操作时会按需要将其置零. ...

  8. golang mysql 模糊查询

    db.SqlDB.Query("SELECT id,name FROM test_table where title name like CONCAT('%',?,'%');", ...

  9. object xml

    http://stackoverflow.com/questions/17739330/xmlserializer-convert-c-sharp-object-to-xml-string http: ...

  10. lnmp配置

    yum源切换 下载wegt工具 yum install -y wget 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...