2012-02-24 53 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/