资讯专栏INFORMATION COLUMN

jQuery源码解析之$().animate()(上)

Batkid / 1323人阅读

摘要:前言需要先看源码解析之和一举例的宽度先变成,再变成,最后变成这是在异步调用中,进行同步调用动画是异步的就是连续调用二作用通过样式将元素从一个状态改变为另一个状态源码之前有说过是的方法源码行是否是空对象,方法执行单个动画的封装的本质是执行

前言:
需要先看 jQuery源码解析之$.queue()、$.dequeue()和jQuery.Callbacks()

一、举例
divA 的宽度先变成 500px,再变成 300px,最后变成 1000px:

  1. 这是A

二、$().animate()
作用:
通过 CSS 样式将元素从一个状态改变为另一个状态

源码:

</>code

  1. //之前有说过: jQuery.fn.extend() 是$()的方法
  2. jQuery.fn.extend( {
  3. //源码8062行
  4. //{"width": "500"}
  5. animate: function( prop, speed, easing, callback ) {
  6. //是否是空对象,false
  7. var empty = jQuery.isEmptyObject( prop ),
  8. // optall={
  9. // complete:function(){jQuery.dequeue()},
  10. // old:false,
  11. // duration: 400,
  12. // easing: undefined,
  13. // queue:"fx",
  14. // }
  15. //undefined undefined undefined
  16. optall = jQuery.speed( speed, easing, callback ),
  17. doAnimation = function() {
  18. // Operate on a copy of prop so per-property easing won"t be lost
  19. //Animation 方法执行单个动画的封装
  20. //doAnimation的本质是执行Animation方法
  21. //this:目标元素
  22. //{"width": "500"}
  23. //optall={xxx}
  24. var anim = Animation( this, jQuery.extend( {}, prop ), optall );
  25. // Empty animations, or finishing resolves immediately
  26. //finish是数据缓存的一个全局变量
  27. //如果为true,立即终止动画
  28. if ( empty || dataPriv.get( this, "finish" ) ) {
  29. anim.stop( true );
  30. }
  31. };
  32. //注意这个
  33. //自身的.finish=自身
  34. doAnimation.finish = doAnimation;
  35. return empty || optall.queue === false ?
  36. this.each( doAnimation ) :
  37. //一般走这里
  38. //通过 queue 调度动画之间的衔接
  39. //optall.queue:"fx"
  40. //doAnimation:function(){}
  41. this.queue( optall.queue, doAnimation );
  42. },
  43. })

解析:
(1)jQuery.speed()
作用:
初始化动画对象的属性

源码:

</>code

  1. //源码8009行
  2. //undefiend undefined undefined
  3. //作用是返回一个经过修改的opt对象
  4. jQuery.speed = function( speed, easing, fn ) {
  5. // opt={
  6. // complete:false,
  7. // duration: undefined,
  8. // easing: undefined,
  9. // }
  10. var opt = speed && typeof speed === "object" ? jQuery.extend( {}, speed ) : {
  11. complete: fn || !fn && easing ||
  12. isFunction( speed ) && speed,
  13. duration: speed,
  14. easing: fn && easing || easing && !isFunction( easing ) && easing
  15. };
  16. // Go to the end state if fx are off
  17. /*这边是为opt.duration赋值的*/
  18. //undefiend
  19. if ( jQuery.fx.off ) {
  20. opt.duration = 0;
  21. } else {
  22. if ( typeof opt.duration !== "number" ) {
  23. if ( opt.duration in jQuery.fx.speeds ) {
  24. opt.duration = jQuery.fx.speeds[ opt.duration ];
  25. } else {
  26. //走这边
  27. //_default=400
  28. opt.duration = jQuery.fx.speeds._default;
  29. }
  30. }
  31. }
  32. /*为opt.queue赋值*/
  33. // Normalize opt.queue - true/undefined/null -> "fx"
  34. //注意这个==,是模糊判断
  35. if ( opt.queue == null || opt.queue === true ) {
  36. opt.queue = "fx";
  37. }
  38. // Queueing
  39. /*为opt.old赋值*/
  40. opt.old = opt.complete;
  41. opt.complete = function() {
  42. if ( isFunction( opt.old ) ) {
  43. opt.old.call( this );
  44. }
  45. //如果queue有值得话,就出队列
  46. //因为queue默认为"fx",所以一般会触发dequeue操作
  47. if ( opt.queue ) {
  48. //this指目标元素
  49. //opt.queue
  50. jQuery.dequeue( this, opt.queue );
  51. }
  52. };
  53. //此时的opt为:
  54. // opt={
  55. // complete:function(){jQuery.dequeue()},
  56. // old:false,
  57. // duration: 400,
  58. // easing: undefined,
  59. // queue:"fx",
  60. // }
  61. return opt;
  62. };

解析:
通过传入的三个参数,对opt对象进行处理,如果三个参数都为undefined的话,opt等于:

</>code

  1. opt={
  2. complete:function(){jQuery.dequeue()},
  3. old:false,
  4. duration: 400,
  5. easing: undefined,
  6. queue:"fx",
  7. }

(2)animate内部做了什么?
作用:
animate内部封装了一个doAnimation触发器,触发器触发就会运行Animation方法,animate最后返回的是queue()方法,注意queue()方法的参数带有触发器doAnimation

(3)$().queue()
作用:
执行入队操作(jQuery.queue()),如果是fx动画的话,同时执行出队操作(jQuery.dequeue()

源码
这个方法上篇文章已经分析过了,这里就简单分析下:

</>code

  1. jQuery.fn.extend( {
  2. //optall.queue:"fx"
  3. //doAnimation:function(){}
  4. //fx动画的话,就执行dequeue(),也就是队首callback函数
  5. queue: function( type, data ) {
  6. //返回的是数组[doAnimate,doAnimate,xxx]
  7. var queue = jQuery.queue( this, type, data );
  8. //初始化hooks
  9. jQuery._queueHooks( this, type );
  10. /*专门为fx动画做处理*/
  11. //如果队首没有锁的话,直接运行队首元素
  12. if ( type === "fx" && queue[ 0 ] !== "inprogress" ) {
  13. jQuery.dequeue( this, type );
  14. }
  15. },

解析:
inprogress是动画队列的锁,目的是保证上个动画执行结束后,再去执行下个动画

每入队一个doAnimate函数,如果队首没有inprogress 锁的话,就会出队去运行一个doAnimate函数

jQuery._queueHooks()的意义在于添加一个empty.remove()方法,用来清空队列queue

(4)jQuery.queue()
作用:
上篇文章也分析过了,就是将doAnimate函数pushqueue数组中

(5)jQuery.dequeue()
作用:
如果队首元素不是inprogress,而是doAnimation方法,则先将doAnimation出队,再让inprogress入队首,再运行doAnimation方法;
如果队首元素是inprogress的话,则移除锁

如果队列为空的话,则清空queue,节省内存

源码:

</>code

  1. //源码4624行
  2. //目标元素,"type"
  3. dequeue: function( elem, type ) {
  4. //"type"
  5. type = type || "fx";
  6. //get,获取目标元素的队列
  7. var queue = jQuery.queue( elem, type ),
  8. //长度
  9. startLength = queue.length,
  10. //去除对首元素,并返回该元素
  11. fn = queue.shift(),
  12. //确保该队列有一个hooks
  13. hooks = jQuery._queueHooks( elem, type ),
  14. //next相当于dequeue的触发器
  15. next = function() {
  16. jQuery.dequeue( elem, type );
  17. };
  18. // If the fx queue is dequeued, always remove the progress sentinel
  19. //如果fn="inprogress",说明是fx动画队列正在出队,就移除inprogress
  20. if ( fn === "inprogress" ) {
  21. fn = queue.shift();
  22. startLength--;
  23. }
  24. if ( fn ) {
  25. // Add a progress sentinel to prevent the fx queue from being
  26. // automatically dequeued
  27. //如果是fx动画队列的话,就添加inprogress标志,来防止自动出队执行
  28. //意思应该是等上一个动画执行完毕后,再执行下一个动画
  29. if ( type === "fx" ) {
  30. queue.unshift( "inprogress" );
  31. }
  32. // Clear up the last queue stop function
  33. //删除hooks的stop属性方法
  34. delete hooks.stop;
  35. //递归dequeue方法
  36. fn.call( elem, next, hooks );
  37. }
  38. console.log(startLength,"startLength4669")
  39. //如果队列是一个空数组,并且hooks存在的话,清除该队列
  40. if ( !startLength && hooks ) {
  41. //进行队列清理
  42. hooks.empty.fire();
  43. console.log(hooks.empty.fire(),"bbbb4671")
  44. }
  45. },

解析:
循环同步运行多个doAnimation()方法,直到队列为空

综上,除doAnimation内的逻辑外,整个$().animate()的流程图为:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/105057.html

相关文章

  • jQuery源码解析$().animate()(下)

    摘要:根据的间隔,利用循环执行,从而达到渲染动画的目的。最后,附上的流程图,建议配合整个的流程图二的最后一个图一起看下篇将会模拟实现方法,敬请期待完 三、doAnimation内部的Animation()方法作用:$().animate()核心方法 源码: //animate()核心方法 //源码7844行 //elem:目标元素 //this:目标元素 //{widt...

    raledong 评论0 收藏0
  • jQuery模拟实现$().animate()(

    摘要:事件在完成过渡后触发,这里当做单个动画完成的信号,触发后,会告知下个动画进行下图的实现将在下篇文章贴出完 showImg(https://segmentfault.com/img/remote/1460000019618970); showImg(https://segmentfault.com/img/remote/1460000019618971); 根据上图实现除doAnimat...

    lcodecorex 评论0 收藏0

发表评论

0条评论

Batkid

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<