jQuery - 使用悬停()的动画闪烁
我有一个如这个 问题 中所述的设置,效果很好。本质上,当您将鼠标移到下拉菜单上以显示更多选项时,下拉菜单就会变大。
然而,有一个小问题。如果您将鼠标移出 #dropdown
div,然后快速返回,它会不断触发 mouseenter
和 mouseleave
事件,导致永无止境闪烁周期。我怎样才能绕过它?
这是我当前的 jQuery 代码
$("#dropdown").hover(function() {
$(this).stop(true,true).fadeTo('fast',1);
$("#options").stop(true,true).slideDown();
}, function() {
$(this).stop(true,true).fadeTo('fast',0.1);
$("#options").stop(true,true).slideUp();
}
);
,当前的 HTML 代码
<div id="dropdown">
<div id="optionsPeek">Options</div>
<div id="options">
<!-- Links here -->
</div>
</div>
dropdown
默认情况下可见(10% 不透明度),optionspeek
始终可见,一旦将鼠标悬停在其上,options
div 向下滑动,其中的链接变得可见。
I have a setup as described in this question which works perfectly. Essentially a drop down menu grows when you move your mouse over it to expose more options.
There is, however, a small issue. If you move the mouse outside of the #dropdown
div and then back in again quickly it constantly fires the mouseenter
and mouseleave
events causing a never ending cycle of flickering. How can I get around it?
Here is my current jQuery code
$("#dropdown").hover(function() {
$(this).stop(true,true).fadeTo('fast',1);
$("#options").stop(true,true).slideDown();
}, function() {
$(this).stop(true,true).fadeTo('fast',0.1);
$("#options").stop(true,true).slideUp();
}
);
And current HTML code
<div id="dropdown">
<div id="optionsPeek">Options</div>
<div id="options">
<!-- Links here -->
</div>
</div>
dropdown
is visible by default (10% opacity), optionspeek
is always visible and once you hover over it, the options
div slides down and the links inside it become visible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果向淡出添加
delay()
会怎么样?例如1-2秒。这样您就可以将鼠标移开并返回到下拉菜单,而不会产生任何动画。http://api.jquery.com/delay/
What if you add a
delay()
to the fadeout? For example 1-2 seconds. That way you can move your mouse away and back onto the dropdown without causing any animations.http://api.jquery.com/delay/
我发现处理此类问题的最佳方法是使用 HoverIntent 插件。它旨在防止您遇到的闪烁问题。
The best way I've found to deal with such issues is to use the HoverIntent plug-in. It was designed to prevent the flicker problems you're having.
使用:
溢出:隐藏;
use:
overflow: hidden;
我似乎没有看到您提到的闪烁,但话又说回来 - 我确实创建了标记,因为您的标记不可用。
I don't seem to be getting the flickering you mentioned, but then again - I did create the markup because yours wasn't available.