JQuery隊列queue與原生模仿其實現

來源:互聯網
上載者:User

標籤:http   color   io   ar   java   sp   div   art   on   

jQuery中的queue和dequeue是一組很有用的方法,他們對於一系列需要按次序啟動並執行函數特別有用。特別animate動畫,ajax,以及timeout等需要一定時間的函數。

queue() 方法顯示或操作在匹配元素上執行的函數隊列。

queue和dequeue的過程主要是:

  1. 用queue把函數排入佇列(通常是函數數組)k7娛樂城
  2. 用dequeue將函數數組中的第一個函數取出,並執行(用shift()方法取出並執行)

也就意味著當再次執行dequeue的時候,得到的是另一個函數了。同時也意味著,如果不執行dequeue,那麼隊列中的下一個函數永遠不會執行。

對於一個元素上執行animate方法加動畫,jQuery內部也會將其加入名為 fx 的函數隊列。而對於多個元素要依次執行動畫,則必須我們手動設定隊列進行了。

一個例子,要兩個div依次向左移動:

01 <div id="block1">div 1</div>
02 <div id="block2">div 2</div>
03 <script type="text/javascript">
04     var FUNC=[
05         function() {$("#block1").animate({color:"=blue"},aniCB);},
06         function() {$("#block2").animate({color:"=red"},aniCB);},
07         function() {$("#block1").animate({color:"=yellow"},aniCB);},
08         function() {$("#block2").animate({color:"=grey"},aniCB);},
09         function() {$("#block1").animate({color:"=green"},aniCB);},
10         function(){alert("動畫結束")}
11     ];
12     var aniCB=function() {
13         $(document).dequeue("myAnimation");
14     }
15     $(document).queue("myAnimation",FUNC)
16     //aniCB();
17 </script>
  1. 我首先建立了一個函數數組,裡邊是一些列需要依次執行的動畫
  2. 然後我定義了一個回呼函數,用dequeue方法用來執行隊列中的下一個函數
  3. 接著把這個函數數組放到document上的myAnimation的隊列中(可以選擇任何元素,我只是為了方便而把這個隊列放在document上)
  4. 最後我開始執行隊列中的第一個函數

這樣做的好處在於函數數組是線性展開,增減起來非常方便。而且,當不要要繼續進行接下來動畫的時候(比如使用者點了某個按鈕),只需要清空那個隊列即可。而要增加更多則只需要排入佇列即可。

1 //清空隊列
2 $(document).queue("myAnimation",[]);
3 //加一個新的函數放在最後
4 $(document).queue(“myAnimation”,function(){alert("動畫真的結束了!")});

這當然也可以用於ajax之類的方法,如果需要一系列ajax互動,每個ajax都希望在前一個結束之後開始,之前最原始的方法就是用回呼函數,但這樣太麻煩了。同樣利用queue添加隊列,每次ajax之後的回調中執行一次dequeue即可。

jQuery中動畫animate的隊列實現,下面用JavaScript模仿一個:

view source print?
01 function Queue(){
02     this.a = [];
03     this.t = null;
04 }
05 Queue.prototype = {
06     queue:function(s){
07         var self = this;
08         this.a.push(s);
09         this.hold();
10         return this;
11     },
12     hold:function(){
13         var self = this;
14         clearTimeout(this.t);
15         this.t = setTimeout(function(){
16             console.log("Queue start! ",self.a);
17             self.dequeue();
18         },0);
19     },
20     dequeue:function(){
21         var s = this.a.shift(),self = this;
22         if(s){
23             console.log("s:"+s);
24             setTimeout(function(){
25                 console.log("end:"+s);
26                 self.dequeue();
27             },s);
28         }
29     }
30 };
31  
32 var a = newQueue().queue(500).queue(200).queue(400).queue(1500).queue(300).queue(2000);

JQuery隊列queue與原生模仿其實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.