jQuery mouseenter/mouseleave 故障

标签 jquery css

当我将鼠标悬停在框中时,它会连续淡出和淡入。我不知道为什么或者是否只在我的浏览器(Siri)上出现这种情况,但你必须稍微尝试一下才能意识到我的问题。

当我留在现场时,我尝试将其淡出,并在离开时再次淡入。

这是fiddle

HTML:

<div class="ani-bounce-slow"></div>

CSS:

div {
    height: 200px; 
    width: 200px; 
    background-color: #3c3;
}

jQuery:

$('.ani-bounce-slow').mouseenter(function() {
    $('.ani-bounce-slow').fadeOut('slow');
});
$('.ani-bounce-slow').mouseleave(function() {
    $('.ani-bounce-slow').fadeIn('slow');
});

最佳答案

当您将鼠标悬停在框上时,它会淡出,因此那里不再有框(因此您的 mouseleave 函数会触发)。

在这种情况下,更好的方法是使用不透明度:

CSS:

div {
    height: 200px; 
    width: 200px; 
    background-color: #3c3;
    transition: opacity 1s;
}
.fade {
    opacity: 0;
}

JS:

$('.ani-bounce-slow').mouseenter(function () {
    $('.ani-bounce-slow').addClass('fade');
})
$('.ani-bounce-slow').mouseleave(function () {
    $('.ani-bounce-slow').removeClass('fade')
});

或者:

$('.ani-bounce-slow').hover(function () {
  $(this).toggleClass('fade');
});

JSFIDDLE

关于jQuery mouseenter/mouseleave 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33378521/

相关文章:

javascript - 使用ajax post请求到geoserver时出错

html - 如何将两侧带有不均匀图标的文本居中对齐?

html - CSS .not(selector) 不适用于 span 标签

css - 使用图像作为输入文件显示输入类型文件中的图像文件名

html - 带剪辑路径的三 Angular 形 ||多余 "clipping"的背景?

javascript - 在表中加载表 - jquery

javascript - CSS\JS\JQuery : Customize color of clicked (not chosen) option in multiselect box

asp.net-mvc - ASP.Net MVC 和 jQuery 表单插件上传文件

javascript - 对于此动画的 "step"事件,使用开关、三元或对象而不是 if/else?

javascript - Vimeo Froogaloop API 无法识别事件