在 jQuery 发生事件后如何做某事?

How to do something after an event in jQuery?

我想在另一个动画完成后播放一个动画。

我使用了这个代码:

$("#test_01").stop().animate({height: '500'}, 2500).promise($("#test_02").fadeIn(2500));

这会同时为两者设置动画 - 这不是我想要的。 我需要 $("#test_01") 先工作,然后 $("#test_02") 需要 运行.

我该怎么做?

尝试 done 使用您的代码:

$("#test_01").stop().animate(
    {height: '500'},
    2500
).promise().done(
    function(){
        $("#test_02").fadeIn(2500)
    }
); 

done 函数回调将在动画中的所有代码完成后起作用。

只需使用 animate()

的完整回调
$("#test_01").stop().animate({
    height: '500'
}, 2500, function () {
    $("#test_02").fadeIn(2500)
});

如果您正在寻找基于 promise 的解决方案,那么您需要获取 promise 对象,然后向将执行延迟操作的 promise 添加一个完成回调