jQuery:在函数a完成后创建函数b
我想知道第一个功能完成后执行第二个功能的最佳方法是什么。
例如,制作一个动画,然后淡入另一个元素:
$('.btn').click(function () {
$("#div1").animate({
marginTop: - 100
}, "slow");
$("#div2").fadeIn();
});
这会在 Div2 中淡入淡出,同时对 div1 进行动画处理。有些人使用delay(),但这不是一个很好的解决方案,因为某些浏览器的行为不同等。
您能为此提出一个好的解决方案吗?谢谢
I was wondering what is the best way to perform a second function after the first one is done.
For example make an animation and then fadeIn another element:
$('.btn').click(function () {
$("#div1").animate({
marginTop: - 100
}, "slow");
$("#div2").fadeIn();
});
This fades in the Div2 and at the same time it animates the div1. Some people use delay() but that's not a very good solution because some browsers act differently etc.
Can you suggest a good solution to this please? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用像 animate 这样的内置函数,您可以利用它的回调函数(动画完成后执行),如下所示
:您可以像这样将回调函数传递给它:
我希望这有帮助!
If you're using a built-in function like animate you can leverage it's callback function (which is executed once the animation is complete) like this:
If you're creating your own function; you can pass the callback function to it like this:
I hope this helps!
动画
[文档]< /a> 接受第三个参数,即动画完成后执行的回调函数:animate
[docs] accepts a third argument, a callback function which is executed once the animation is complete:您可以在 div1 的 animate 完整函数中执行 div2.fadeIn() 。
You can perform div2.fadeIn() in the complete function of animate for div1.
您可以使用回调。一旦第一个函数返回,第二个函数就会启动。您可以根据需要链接任意数量的回调。
@felix-kling 在 他的帖子中有一个示例
You can use Callbacks. As soon as the first function has returned, the second one is started. You can chain as many callbacks as you want.
@felix-kling has an example in his post