jquery - 在 jquery 中停止父函数时防止回调被停止?

标签 jquery animation callback jquery-callback

事情有点像这样:

$("example").hover(function(){
        ("example").stop().show(/*More Code*/, callback());
    }, function(){
        ("example").stop().hide(/*More Code*/, callback());
    }
)

因此,如果鼠标在第一个动画完成之前离开该区域,它将停止所有动画,并跳过回调。有没有办法使用 stop() 并仍然执行回调函数?

最佳答案

使用 setTimeout它定时在动画结束时触发,而不是回调。

$("example").hover(function(){
        $("example").stop().show(1000);
        setTimeout(function() { /* do something */ }, 1000);
    }, function(){
        $("example").stop().hide(1000);
        setTimeout(function() { /* do something */ }, 1000);
    }
);

另一种选择是使用 .stop(true,true) 而不仅仅是 .stop()

这会清除队列,将动画发送到末尾,并触发回调。

$("example").hover(function(){
        $("example").stop(true,true).show(1000);
    }, function(){
        $("example").stop(true,true).hide(1000);
    }
);

关于jquery - 在 jquery 中停止父函数时防止回调被停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4812615/

相关文章:

javascript - jQuery Roundabout - 元素位置错误

jquery - 添加指向所选图像的链接

javascript - 获取每个 div 的所有选择值,其中多个 div 具有相同的类名

jquery - 为什么我的悬停效果没有动画?

qt - QGraphicsItem - 跟随鼠标光标

java - 没有回调接口(interface)的AsyncTask

python - 在 ctypes 回调函数中使用线程锁

javascript - 触发器不适用于 .change jQuery 上添加的动态内容

javascript - 如何在页面未完全加载时显示正在加载的 gif

jquery - 无尽的水平 jQuery 滚动条