javascript - 并发动画结束时的jquery

标签 javascript jquery animation

所以我在一个元素上运行两个并发动画,我想在两个动画结束后做一些事情

        $("#MyDiv").stop().animate({left: "-=" + 500 + "px"}, 1000);
        $("#MyDiv").animate({top: "-=" + 500 + "px"}, 1000, 

function() {
//do something here
});

这就是我的方法。我想知道是否有更好的方法,因为第一个动画可能会提前或延迟几毫秒结束。

第二个问题:.stop().animate() 和 .animate() 之间有什么区别吗?如果是,那是什么?

提前致谢!

最佳答案

"because the first animation may end a few milliseconds earlier or later"

不,您已将两个动画设置为 1000 毫秒。

"is there any difference between .stop().animate() and .animate() ? If yes, what is it ?"

是的,如果动画处于事件状态并且调用了 stop(),则第一个动画将停止。

因为您正在为同一个元素设置动画,所以您不需要选择它两次。我建议你这样做:

$("#MyDiv").stop(true).animate({left: "-=" + 500 + "px", top: "-=" + 500 + "px"}, 1000, 
function(){
    //animation complete
});

如果动画“卡住”,您可以向停止方法添加第二个 bool 值,stop(true, true) ("jumpToEnd")。第一个将清除动画队列。如果不查看更多代码,很难说出您需要什么。

关于javascript - 并发动画结束时的jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553077/

相关文章:

javascript - jquery 在鼠标悬停时停止图像旋转,在鼠标悬停/悬停时开始旋转

ios - UIView.animateWithDuration : animation always stops at original position

javascript - 如何使用 javascript 或 jquery 在表中创建多个元素

javascript - 使用 Vuejs 指令和 i18n 包装 Selectpicker

javascript - lodash 函数的前缀

php - 单击时更改按钮文本并再次单击时更改回来

javascript - 移除/删除 HTML 页面中的 ":"冒号

javascript - Angular 如何重新加载 Controller

javascript - JQuery 脚本未正确检查复选框

javascript - jQuery + Animate.css 动画只工作一次,动画不重置