2013-03-14 181 views
4
$('.ip_button').click(function(){ 
    var buttonValue, slideTimes; 
    buttonValue = $(this).html(); 
    slideTimes = parseInt(buttonValue) - 1; 
    for (var i = 0 ; i < slideTimes; i++) { 
      slider.gallery_next(); 
     }    
}); 

您好我想滑動多個圖像滑塊使用函數,確定按鈕-1的值和運行幻燈片多次。不幸的是,無論按鈕中有什麼值,函數都只能執行一次。當前按鈕中的值爲:循環迭代函數x次循環

<button class="ip_button">5</button> 

gallery_next()函數;封裝和是這樣的:

this.gallery_next = function() { 
    if (galleryRotating) { return; } 
    galleryRotating = true; 
    var elem = $(galleryId + " > li").first(); 
    $(galleryId).append(elem.clone()); 
    $(galleryId).animate({"left" : -imgWidth}, 650, function() { 
     elem.remove(); 
     galleryRotating = false; 
     $(galleryId).css({"left" : 0}); 
    }); 

回答

3

animate不會因時間for循環再次調用函數完成,所以galleryRotation仍然是真實的,並且功能會做什麼。你需要一個不同的方法:設置一個計時器來調用你的函數,比如700ms或者(更好)在你傳遞給animate的回調中再次調用旋轉。

+0

請檢查: http://jsfiddle.net/inser/LwhhD/3/ – inser 2013-03-14 13:33:04

+0

它實際上是放入隊列:http://api.jquery.com/animate/ 在你的問題你做檢查動畫效果是否已經激活,因此動畫未被排入隊列。在jsfiddle示例中,您不檢查它,所以動畫排隊。 – Aneri 2013-03-14 13:40:54