2012-11-26 41 views
1

我有兩個jQuery的動畫一個由其它:jquery動畫 - 他們什麼時候完成?

 books.animate({ left: left1 }, { 
      duration: this.slideDuration, 
      easing: this.easing, 
      complete: complete 
     }); 

     laptops.animate({ left: left2 }, { 
      duration: this.slideDuration, 
      easing: this.easing, 
      complete: complete 
     }); 

我想要的動畫simultanusly運行,因此我使用{queue: false}

books.animate({ left: left1 }, { 
      duration: this.slideDuration, 
      easing: this.easing, 
          queue: false, 
      complete: complete 
     }); 

     laptops.animate({ left: left2 }, { 
      duration: this.slideDuration, 
      easing: this.easing, 
          queue: false, 
      complete: complete 
     }); 

但現在已完成回調叫了兩聲!我怎樣才能確切地知道兩種動畫何時完成?

回答

1

使用jQuery推遲方法試圖

$.when( 
    books.animate({ left: left1 }, { 
     duration: this.slideDuration, 
     easing: this.easing 
    }), 
    laptops.animate({ left: left2 }, { 
     duration: this.slideDuration, 
     easing: this.easing 
    }) 
).done(function() { 

    alert("done!"); 

}); 

小提琴here

+0

我看不出這會如何工作? jQuery如何理解匿名函數內的動畫何時完成? – andlrc

+0

@NULL你是絕對正確的。謝謝你。我更正了我的代碼。 – Bruno

+0

@NULL:它看起來有生命力返回延遲對象,它可以與when一起使用。我工作很棒。 – Naor

1

爲什麼不從一個動畫中刪除complete處理程序?

從您發佈的代碼摘錄中,它看起來好像在兩個動畫中使用了相同的持續時間和緩動方法。因此,他們將同時完成,只要他們同時被呼叫,這是固有的真實...

+0

在這種情況下,我真的可以指望持續時間,但如果明天有人會改變持續時間,代碼將無法正常工作。假設有兩個Ajax調用,我們需要知道他們什麼時候完成。 – Naor