2011-12-13 115 views
4

爲什麼它不會觸發警報?jQuery動畫回調不起作用

var $anchor = $(this); 

$('.hide').val($(this).attr('href')); 
$('html, body').animate({ 
    scrollLeft: $($anchor.attr('href')).offset().left 
}, { 
    queue: false, 
    duration: 1000, 
    easing: 'easeInOutCirc' 
}, function() { 
    alert('test'); 
}); 

回答

8

有您能與.animate()使用多個不同的語法選項。當你傳遞一個對象的屬性和選項對象(如你正在做的),完成函數雲在選擇對象不是這樣的第三個參數:

var $anchor = $(this); 

$('.hide').val($(this).attr('href')); 
$('html, body').animate({ 
    scrollLeft: $($anchor.attr('href')).offset().left 
    }, { 
    queue: false, 
    duration: 1000, 
    easing: 'easeInOutCirc', 
    complete: function() { 
     alert('test'); 
    } 
    } 
); 

這在滿是jQuery .animate() doc描述。

.animate(properties, options) 

properties - A map of CSS properties that the animation will move toward. 

options - A map of additional options to pass to the method. Supported keys: 
    duration: A string or number determining how long the animation will run. 
    easing: A string indicating which easing function to use for the transition. 
    complete: A function to call once the animation is complete. 
    step: A function to be called after each step of the animation. 
    queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option can also accept a string, in which case the animation is added to the queue represented by that string. 
    specialEasing: A map of one or more of the CSS properties defined by the properties argument and their corresponding easing functions (added 1.4). 
+0

對不起,遲到的答覆和謝謝。 – jQuerybeast

+1

對於這樣一個荒謬的問題標題,這個答案是最有幫助的! – nipponese

2

嘗試指定第三個參數爲「完全」,例如:

var $anchor = $(this); 

$('.hide').val($(this).attr('href')); 
$('html, body').animate({ 
    scrollLeft: $($anchor.attr('href')).offset().left 
}, { 
    queue: false, 
    duration: 1000, 
    easing: 'easeInOutCirc' 
}, complete: function() { 
    alert('test'); 
}); 
+0

我的html很好,它的編譯正確,沒有函數。我沒有得到任何錯誤。你的錯誤是因爲.hide沒有任何屬性。 – jQuerybeast

+0

好吧,我認爲這是'完整'參數(請參閱上面的更新代碼) – themerlinproject

+0

這是不正確的。你可以在上面看到'complete:'參數所在的位置。它不是動畫函數的第三個參數,而是第二個參數的屬性。 – jfriend00