在动画/停止时改变速度(持续时间)

时间:2011-12-21 15:05:43

标签: javascript jquery performance duration

环顾四周,找不到答案。我需要在此代码中添加动画的持续时间:

$('a:has(.image-active)').hover(function() { 
    $('.image-active', this).stop().animate({"opacity": 1}); 
},function() { 
    $('.image-active', this).stop().animate({"opacity": 0}); 
});

但是,我无法确定在何处放置持续时间。刚才它淡入1并淡出为0,这必须是jQuery的默认值。

4 个答案:

答案 0 :(得分:2)

将持续时间作为第二个参数传递给animate(),例如:

.animate({"opacity": 1}, "fast");

.animate({"opacity": 1}, 3000);

答案 1 :(得分:1)

.animate()有一个参数对象:

.animate({"opacity": 1}, {duration: 100});

我喜欢冗长,但您也可以将持续时间作为数字传递:

.animate({"opacity": 1}, 100);

阅读documentation了解更多选项。看看这些例子。

答案 2 :(得分:0)

jquery API for the .animate() method是一个很好的起点。您可以在属性之后为动画指定持续时间(作为毫秒值,或“快速”或“慢”字符串)。如果我没记错的话,默认值是400。

答案 3 :(得分:0)

首先,opacity不应该quotes更改为"opacity"opacity

.animate({opacity: 1},100);

参考here