javascript - 由于过多的事件,jQuery 动画表现异常?

标签 javascript jquery css animation jquery-animate

我的网站上有一个画廊。每张图片都是一个 <div>它有一个背景图像。溢出是隐藏的,我隐藏了一个标题 div使用 margin 。然后,我使用以下 jQuery 在鼠标进入和离开图片时为字幕设置动画 <div> .

$(document).on("mouseenter", ".gallery-image", function(){
    $(this).children(".caption").dequeue();
    $(this).children(".caption").fadeIn({queue: false, duration: 500}).animate({marginTop: "350px"}, 500);
});
$(document).on("mouseleave", ".gallery-image", function(){
    $(this).children(".caption").dequeue();
    $(this).children(".caption").fadeOut({queue: false, duration: 500}).animate({marginTop: "400px"}, 500);
});

当我将鼠标移入和移出太快时,奇怪的事情开始发生。标题保持半褪色,或者标题完全停止出现。

问题可以看这个JSFiddle .

为什么我会遇到这种意外行为?

最佳答案

使用.stop(true, true) 停止动画的预排队

$(document).on("mouseenter", ".gallery-image", function(){
        $(this).children(".caption").stop(true,true).fadeIn({queue: false, duration: 500}).animate({marginTop: "350px"}, 500);
 });
    $(document).on("mouseleave", ".gallery-image", function(){
        $(this).children(".caption").stop(true,true).fadeOut({queue: false, duration: 500}).animate({marginTop: "400px"}, 500);
 });

Fiddle

关于javascript - 由于过多的事件,jQuery 动画表现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745291/

相关文章:

javascript - 检查是否选择了选项 - 具有多个选择

javascript - 如何通过点击按钮来增加div的marginLeft

javascript - 使用 javascript/jquery 在 div 顶部的进度条加载器

html - 我如何强制 iOS 设备跟随我的 CSS

html - display flex 不会像预期的那样打断长行

javascript - 哪些浏览器支持getElementById?

javascript - Express.js : Match route in catch all

javascript - 如何在javascript中获取UTC时区并调整为PDT/PT

javascript - 使用 jQuery Ajax 请求交换图像 src

html - 如何为不同的网络浏览器修复视频大小和字体换行