繁体   English   中英

jQuery mouseenter和mouseleave淡入防止延迟重复

[英]jQuery mouseenter and mouseleave fadeTo prevent delayed repeat

因此,我有一个div,它在鼠标输入时逐渐消失,在mouseleave上逐渐下降,这很好用。

$('.myDiv').mouseenter(function(){
$(this).fadeTo('slow', 1);
});

$('.myDiv').mouseleave(function(){
$(this).fadeTo('slow', 0.4);
});

参见jsfiddle

但是,如果您快速反复地前后移动鼠标几次,随着动画继续运行,div将“闪烁”。 有办法阻止这种情况发生吗?

我已经尝试过回调,但是没有达到预期的效果。

有什么建议么?

尝试:

$('.myDiv').mouseenter(function(){
    $(this).stop();
    $(this).fadeTo('slow', 1);

});
$('.myDiv').mouseleave(function(){
    $(this).stop();
    $(this).fadeTo('slow', 0.4);
});

https://jsfiddle.net/1Lrcubwp/

更好的方法是使用CSS。 不应将Javascript用于此类动画,因为它会使您的网站变慢。 请参阅下面的示例。

 .fade { background-color: red; width: 200px; height: 200px; opacity: 0.4; filter: alpha(opacity=40); /* For IE8 and earlier */ transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; cursor: pointer; } .fade:hover { opacity: 1; } 
 <div class='fade'></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM