jQuery:在函数a完成后创建函数b

发布于 2024-12-12 21:14:30 字数 318 浏览 4 评论 0原文

我想知道第一个功能完成后执行第二个功能的最佳方法是什么。

例如,制作一个动画,然后淡入另一个元素:

$('.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 技术交流群。

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

发布评论

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

评论(4

家住魔仙堡 2024-12-19 21:14:31

如果您使用像 animate 这样的内置函数,您可以利用它的回调函数(动画完成后执行),如下所示

$('#div1').animate({marginTop: -100}, "slow", function() {
    $('#div2').fadeIn();
});

:您可以像这样将回调函数传递给它:

function doSomething(param, fn){
    // Do something with function

    // Execute Callback Function //
    if (typeof fn == 'function') {
        fn.call(this); // Give Function Scope
    }
}

doSomemthing(
    param,
    function(){
        // Callback Function Goes Here
    }
)

我希望这有帮助!

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:

$('#div1').animate({marginTop: -100}, "slow", function() {
    $('#div2').fadeIn();
});

If you're creating your own function; you can pass the callback function to it like this:

function doSomething(param, fn){
    // Do something with function

    // Execute Callback Function //
    if (typeof fn == 'function') {
        fn.call(this); // Give Function Scope
    }
}

doSomemthing(
    param,
    function(){
        // Callback Function Goes Here
    }
)

I hope this helps!

多情癖 2024-12-19 21:14:31

动画 [文档]< /a> 接受第三个参数,即动画完成后执行的回调函数:

$("#div1").animate({marginTop: - 100}, "slow", funciton() {
    $("#div2").fadeIn();
});

animate [docs] accepts a third argument, a callback function which is executed once the animation is complete:

$("#div1").animate({marginTop: - 100}, "slow", funciton() {
    $("#div2").fadeIn();
});
深海蓝天 2024-12-19 21:14:31

您可以在 div1 的 animate 完整函数中执行 div2.fadeIn() 。

    $('.btn').click(function () {   
$("#div1").animate({
    marginTop: - 100
    }, "slow", function() {;
    $("#div2").fadeIn();});
});

You can perform div2.fadeIn() in the complete function of animate for div1.

    $('.btn').click(function () {   
$("#div1").animate({
    marginTop: - 100
    }, "slow", function() {;
    $("#div2").fadeIn();});
});
却一份温柔 2024-12-19 21:14:31

您可以使用回调。一旦第一个函数返回,第二个函数就会启动。您可以根据需要链接任意数量的回调。

@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

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