有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)

Is there any way we can stop the animate in Jquery (also stop the complete call back function)

本文关键字:也可以 函数 回调 中的 方法 什么 有没有 Jquery animate      更新时间:2023-09-26

我认为这个问题与Jquery中的队列有关。我们可以使用clearQueue()来清除队列以停止任何动画,但是清除队列不能停止回调函数考虑这个

$("#sth").animate( { width:'100%' }, 2000, function() {
    $("#sth").css("width","0%");
});

$("#sth").clearQueue().css("width","0%");

能够停止动画并向后推宽度。然而,在2000毫秒之后,它也将变为100%宽度,因为有完整的回调功能。无论队列是否存在,该函数都将在2000毫秒后调用。

来自jQuery文档:

.clearQueue()从队列中删除所有尚未运行的项目。

.stop()停止匹配元素上当前正在运行的动画。

请尝试使用.stop()代替.clearQueue().

调用停止函数将停止附加在对象上的动画

function stopSthAnim(){
    $('#sth#).stop(); 
}

你也可以使用完整的回调功能

$("#sth").animate( { width:'100%' }, {
        duration:2000,
        complete: function(){
           $('#sth#).stop(); 
        }
    }, function() {
    $("#sth").css("width","0%");
});
相关文章: