2012-02-24 52 views
0

我的功能看起來像jQuery的動畫問題

$.fn.animateHighlight = function(highlightColor, originalColor, type, duration) { 
    var highlightBg = highlightColor || "#FFFF9C"; 
    var animateMs = 500; 
    var animateVal1 = {}; 
    animateVal1[type] = highlightColor; 
    var animateVal2 = {}; 
    animateVal2[type] = originalColor; 
    this.stop().animate(animateVal1, animateMs).delay(duration).animate(animateVal2, animateMs); 
}; 

調用此類似

$("#variants").animateHighlight("red", "#9c9c9c", "borderColor", 3000); 

的問題是,

這是字段集

enter image description here

的默認邊框顏色

這是動畫色彩

enter image description here

後,我知道,動畫增加了額外的風格屬性,元素。我想要做的是,返回原始字段集邊框顏色(刪除樣式屬性將返回原始邊框顏色)。

試過的功能最後一行改成這樣

this.stop().animate(animateVal1, animateMs).delay(duration).animate(animateVal2, animateMs).removeAttribute('style'); 

動畫甚至沒有開始。

如何在動畫後動畫並重置回原始版本?

回答

0
this.stop().animate(animateVal1, animateMs).delay(duration).animate(animateVal2, animateMs, function() { 
    this.removeAttribute('style'); 
}); 
+0

我可以直接和動畫重置?我的意思是不使用第二種顏色,而是直接使用原始狀態? – heron 2012-02-24 05:46:28

+0

不知道,試着將邊框顏色設置爲透明,看看它是否有效,你可能不得不使用rgba顏色。 – adeneo 2012-02-24 06:04:31

0

我覺得這兩種方法。

1.如果您在VariantDiv中使用stye屬性的邊框顏色.Default樣式在刪除樣式屬性時將以樣式丟失。所以你應該保持第一個邊框顏色。

例如:

http://jsfiddle.net/tEwa9/

2.如果當你不使用樣式,你可以做這樣。 當動畫完成時,您可以調用此代碼。

$(this).attr('style',''); 

例如:

http://jsfiddle.net/xSYWS/