使用 Mootools 链接 AddClass/RemoveClass 事件
我目前正在制作一个 CSS 动画,实现这一目标的一部分涉及以特定的时间间隔更改主体的类名。
作为 mootools(和一般的 js)的新手,我想到实现这一点的最好方法是简单地以延迟的时间间隔向主体添加/删除类,如下所示:
(function() {$(document.body).addClass('load');}).delay(20);
(function() {$(document.body).addClass('load-two');}).delay(2000);
(function() {$(document.body).addClass('load-three');}).delay(2700);
(function() {$(document.body).addClass('load-four');}).delay(4500);
然而,我对这个主题了解得越多,我更确信这是实现我想要的目标的低效方法。
上面的代码适用于我测试过的所有浏览器,但是使用链类来实现我想要的效果会更好吗?我已经查看了有关设置链的 Mootools 文档,但无论出于何种原因,我都在努力让演示正常运行。
所以我要问的关键是,是否有更好的方法来编写上面发布的代码,以及使用不同方法的好处是什么?
谢谢。
I'm currently putting together a css animation, and part of achieving this involves changing the class name of the body at specific intervals.
Being quite new to mootools (and js in general) the best way I've thought of achieving this has been to simply add/remove classes to the body at delayed intervals, like so:
(function() {$(document.body).addClass('load');}).delay(20);
(function() {$(document.body).addClass('load-two');}).delay(2000);
(function() {$(document.body).addClass('load-three');}).delay(2700);
(function() {$(document.body).addClass('load-four');}).delay(4500);
However the more I read up on the subject, the more I'm convinced that this is an inefficient way of achieving what I want to.
The above code works in all the browsers I've tested it in, but would I be better using a chain class to achieve what I want? I have looked over the Mootools docs on setting up a chain, but for whatever reason, I'm struggling to get the demo working.
So the crux of what I'm asking, is if there's a better way of writing the code posted above, and what are the benefits of using a different method?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 mootools 中设置链非常简单。然而,使用 Chain 类作为 mixin 可能会涉及更多一些。
通常,它适合链接基于 Fx 的类和方法,而不是同步的类和方法。假设您有一个正在播放
link: chain
的补间效果,您可以.chain(function() {})
实例在之后执行其他操作。callChain 示例 作为一个独立的例子是很好和简单的,但它在时间控制方面提供的很少。
然后是线性时间线方法。在您的情况下,您的第一个回调在 20 毫秒后运行,1980 毫秒后运行,第二个、第三个回调在第二个回调后 1680 毫秒运行,依此类推。如果您将事情串联起来,以便每个连续的步骤都调用下一个步骤,那么您需要考虑到这一点,并实际上传递两个操作之间等待的时间。
另一种方法是推迟它们,就像你从一开始就做的那样。
我在这里尝试简化前者: http://jsfiddle.net/dimitar/mpzzq/
因此,在我的 funcs 对象数组中,我定义了 func 回调、要传递的参数和延迟。请记住,func 本身将
this
范围设置为链实例,并自行调用链上的下一个,但您可以轻松修改它并使用它。希望它能给你一些想法。
这里是在 take 2 处,带有一个装饰器函数,该函数在延迟时调用链:
示例: http: //jsfiddle.net/dimitar/Y4KCB/4/
setting up a chain in mootools is quite simple. however, using the Chain class as a mixin can be a little more involved.
typically, it's geared up towards chaining of Fx-based classes and methods and not ones that are synchronous. say you have a tween effect which has
link: chain
in play, you can.chain(function() {})
the instance to do something else after.the callChain example as a standalone is fine and easy enough but it offers little in terms of timing controls.
then there's the linear timeline approach. in your case, your first callback runs after 20 ms, 1980 ms after that the second, third runs 1680 ms after the second and so forth. if you chain things so that each successive step calls the next one, you need to take that into account and actually pass on the time to wait between the 2 actions instead.
the other way to do so is to just defer them as you have done from the start.
I had a go at streamlining the former a little here: http://jsfiddle.net/dimitar/mpzzq/
so in my funcs array of objects i define the func callback, the arguments to pass on and the delay. keep in mind that the func itself has the
this
scope set to the chain instance and self calls next one on the chain but you can easily mod this and work with it.hope it gives you some ideas.
here it is at take 2 with a decorator function that calls the chain on a delay:
example of that: http://jsfiddle.net/dimitar/Y4KCB/4/