如何在 jquery 中同时为两件事制作动画
当鼠标悬停在某物上时,我想同时为两件事制作动画。
例如,当鼠标悬停在 id="trigger" 的 div 上时,我想更改 id="box1" 的 box1 的背景颜色和 id="box2" 的 box2 的位置。
但我不希望它们在队列中动画,即一个接一个。我希望他们同时开始动画并同时完成!
I want to animate 2 things at same time when mouse hovers something.
for example I want to change background color of box1 with id = "box1" and position of box2 with id="box2" when mouse hovers a div with id="trigger".
but I don't want them to animate in a queue, i.e. one after another. I want them to start animating at same time and finish at same time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以连续运行它们,如下所示:
这使用
.hover()
< /a> 在悬停时以一种方式设置动画,并在离开时将它们设置为返回。.stop()
只是为了防止动画队列堆积。要对颜色进行动画处理,您需要颜色插件,或jQuery UI 也包括在内。jquery 中的动画,
.animate()
,使用setInterval()
具有 13ms 计时器,因此它们会在正常流程之外发生......像上面那样执行它们不会'不必等待第一个完成,它们将同时运行。You can just run them in a row, like this:
This uses
.hover()
to animate one way when hovering, and animate them back when leaving. The.stop()
is just to prevent animation queue build-up. To animate a color, you'll need either the color plugin, or jQuery UI included as well.Animations in jquery,
.animate()
, are implemented usingsetInterval()
with a 13ms timer, so they'll happen outside the normal flow...doing them like above doesn't wait for the first to finish, they'll run simultaneously.当第一个动画开始时,下一行代码将执行,而无需等待动画完成。
所以要同时做两件事:
When the first animation starts, the next line of code executes without waiting for the animation to finish.
So to do two things at once:
查看 jQuery.animate() 文档。有
queue
属性:Check out jQuery.animate() docs. There is
queue
property:虽然连续调用 animate 确实会产生它们同时运行的外观,但潜在的事实是它们是非常接近并行运行的不同动画。
为了确保动画确实同时运行,请使用:
可以将更多动画添加到“触发悬停”队列中,并且只要最后一个动画出队,就可以启动所有动画。
干杯,
安东尼
While it's true that consecutive calls to animate will give the appearance they are running at the same time, the underlying truth is they're distinct animations running very close to parallel.
To insure the animations are indeed running at the same time use:
Further animations can be added to the 'trigger-hover' queue and all can be initiated provided the last animation dequeue's them.
Cheers,
Anthony