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

发布于 2024-09-06 03:16:14 字数 631 浏览 8 评论 0 原文

jQuery 1.4.2 的 animate() API 规范 是,

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

但似乎我们可以提供 duration callbackno easing

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

就可以了。

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

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

那么缓动将不会生效。那么 API 到底应该是什么?

更新:请参阅下面我的回答。

jQuery 1.4.2's animate() API spec is

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

but it seems that we can supply duration, callback, and no easing

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

and it will work.

But if we supply easing and callback and no duration

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

then the easing won't be taken into effect. So what exactly is the API supposed to be?

Update: please see my answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-09-13 03:16:14

通常 jQuery 按类型确定可选参数,例如,这是可行的:

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

但在持续时间/缓动的情况下,因为它们都是字符串,所以不能(如果有单个字符串,则 它假设它是持续时间)。您需要使用 'normal' 作为持续时间来调用它,如下所示:

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

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

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

Normally jQuery determines optional parameters by types, e.g. this works:

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

But in the case of duration/easing, since they're both strings it can't (and if there's a single string, it assumes it's the duration). You need to either call it with 'normal' as the duration, like this:

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

Or use the .animate(options, properties) version, like this:

.animate({left: '+=100'}, { easing: 'swing', complete: doThis })
梦屿孤独相伴 2024-09-13 03:16:14

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

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

2) 提供 undefined, "swing", doThis 和有用。提供 null, "swing", doThis 并且它也可以工作。尽管如果没有以这种方式记录的话,依赖这种方法可能并不好。如果省略参数,undefined 才是真正的方法,但有时人们说使用 null,即使它不是正式正确的。

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

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

even though I am still not so sure about the API spec, these are some viable solutions:

1) supply 400, "swing", doThis, since 400 seems to be the default duration (look for fast: in the jquery code and you can see fast is 600, slow is 200 as in the spec, and _default is 400.

2) supply undefined, "swing", doThis and it works. Supply null, "swing", doThis and it works too. Although maybe it is not good to depend on this method if it is not documented this way. undefined is the really way to go if argument is omitted but sometimes people say use null even though it is not officially correct.

3) just use {easing: "swing"} and that's the official way to skip the duration and provide a easing method. if callback is needed, then use {easing: "swing", complete: doThis}

Solution 3 seems to be the best, as it doesn't use what the vague part of the spec and go totally with the clearer part of the spec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文