jQuery的animate()参数的确切规则是什么?

时间:2010-06-13 04:42:47

标签: jquery animation effects

jQuery 1.4.2的animate() API spec

.animate( properties, [ duration ], [ easing ], [ callback ] )

但似乎我们可以提供durationcallback easing

.animate({left: '+= 100'}, 600, doThis)

它会起作用。

但是,如果我们提供easingcallback以及 duration

.animate({left: '+=100'}, 'swing', doThis)

那么宽松政策将不会生效。那究竟是什么API应该是什么?

更新:请参阅下面的答案。

2 个答案:

答案 0 :(得分:2)

通常,jQuery按类型确定可选参数,例如这有效:

$('.class').animate({opacity: 'toggle'}, doThis);

但是在持续时间/缓和的情况下,因为它们都是字符串,所以它不能(如果有一个字符串,it assumes it's the duration)。您需要使用'normal'作为持续时间来调用它,如下所示:

.animate({left: '+=100'}, 'normal', 'swing', doThis)

或使用.animate(options, properties)版本,如下所示:

.animate({left: '+=100'}, { easing: 'swing', complete: doThis })

答案 1 :(得分:0)

即使我对API规范仍然不太确定,这些是一些可行的解决方案:

1)提供400, "swing", doThis,因为400似乎是默认持续时间(在jquery代码中查找fast:,你可以看到快速为600,慢速为200,如规范中所示,并且_default是400。

2)提供undefined, "swing", doThis并且它有效。供应null, "swing", doThis,它也有效。虽然如果不以这种方式记录,依靠这种方法可能并不好。如果省略参数,undefined是真正的方法,但有时人们会说使用null,即使它不是正式的。

3)只使用{easing: "swing"},这是跳过持续时间并提供缓动方法的官方方式。如果需要回调,请使用{easing: "swing", complete: doThis}

解决方案3似乎是最好的,因为它没有使用规范的模糊部分,而是完全采用规范中更清晰的部分。

相关问题