jQuery中的queue和dequeue是一组很有用的方法,他们对于一系列需要按次序运行的函数特别有用。特别animate动画,ajax,以及timeout等需要一定时间的函数

queue和dequeue的过程主要是:
1,用queue把函数加入队列(通常是函数数组)
2,用dequeue将函数数组中的第一个函数取出,并执行(用shift()方法取出并执行)

也就意味着当再次执行dequeue的时候,得到的是另一个函数了
同时也意味着,如果不执行dequeue,那么队列中的下一个函数永远不会执行

对于一个元素上执行animate方法加动画,jQuery内部也会将其加入名为 fx 的函数队列
而对于多个元素要依次执行动画,则必须我们手动设置队列进行了。




<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery.data demo</title> <style> div { background:#aaa; width:18px; height:18px; position:absolute; top:10px; } </style> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> </head> <body> <div id="block1"></div> <div id="block2"></div> <script> var FUNC=[ function(){ $("#block1").animate({left:"+=100"},aniCB); }, function(){ $("#block2").animate({left:"+=100"},aniCB); }, function(){ $("#block1").animate({left:"+=100"},aniCB); }, function(){ $("#block2").animate({left:"+=100"},aniCB); }, function(){ $("#block1").animate({left:"+=100"},aniCB); }, function(){ alert("动画结束"); } ]; var aniCB=function(){ $(document).dequeue("myAnimation"); } $(document).queue("myAnimation",FUNC); aniCB(); </script> </body> </html>

  

posted @ 2015-03-25 16:04  Mi文  阅读(235)  评论(0编辑  收藏  举报