Mouseout 和 Mouseenter jquery 函数
我使用了 jQuery mouseout 和 mouseenter 函数。但效果并不好。因为当你快速浏览这些物品时。我得到了非常疯狂的效果。我使用了这段代码:
$('.hover').css('opacity', 1);
$('.controlNav li').mouseover(function() {
$('.hover', this).css({ display: 'block' }).animate({ top: -73, opacity: 1 }, 450, 'swing' );
}).mouseout(function(){
$('.hover', this).css({ display: 'none' }).animate({ top: -60, opacity: 0 });
});
我该如何解决我的问题?
I used the jQuery mouseout and mouseenter function. But is not working good. Because when you go fast over the items. I get verry crazy effects. I used this code:
$('.hover').css('opacity', 1);
$('.controlNav li').mouseover(function() {
$('.hover', this).css({ display: 'block' }).animate({ top: -73, opacity: 1 }, 450, 'swing' );
}).mouseout(function(){
$('.hover', this).css({ display: 'none' }).animate({ top: -60, opacity: 0 });
});
How can i fix my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在动画之前添加了 .stop() ,这将停止动画并应该停止跳跃。
I added in
.stop()
just before the animation which will stop the animation in place and should stop the jumping.问题最初不是
mouseout
或mouseover
事件。他们正在按照应有的方式工作。这意味着即使您将鼠标悬停在该元素上仅1ms
,它也会起作用。此问题的解决方案是延迟操作。您应该等待一定的毫秒数才能执行您想要发生的操作。
您可以手动执行此操作,也可以在实现中使用 jQuery悬停意图插件这非常好并且易于使用。
最好不要使用
mouseout
或mouseover
事件并使用 jQuery.hover()
(如果您使用的是.hoverIntent 中的插件()
以获得更干净、可读的代码。The problem originaly is not
mouseout
ormouseover
events. They are working as they should to work. It means even if you mouse over the element for just1ms
it will work.Solution for this problem is delaying the action. You should wait a certain amount of miliseconds to do what you want happens.
You can do it manually or you can just use jQuery hover intent plug in that implemented this very nice and easy to use.
It's better to not use
mouseout
ormouseover
event and use jQuery.hover()
(if you are using the plug in.hoverIntent()
for more clean and readable code.将某个变量设置为互斥体,例如:
set some variable as mutex, like:
.mouseover()
和.mouseout()
会产生奇怪的结果,因为当鼠标仍在元素内时mouseover()
会多次触发。简单的鼠标移动将再次触发它&再次。.mouseenter()
和.mouseleave()
更好,因为它们只在进入或离开元素时触发一次。然而,它们的功能似乎仍然不如.hover()
将它们组合到一个方法中。另外添加
.stop()
将在开始新动画之前停止当前动画。.stop(true, false)
将清除动画队列中的所有内容,并且不允许当前动画完成。http://api.jquery.com/hover/
http://api.jquery.com/stop/
.mouseover()
and.mouseout()
give strange results becausemouseover()
fires more than once while your mouse is still inside the element. Simple mouse movement will trigger it again & again..mouseenter()
and.mouseleave()
are better because they are only supposed to fire one time upon entering or leaving the element. However, they still do not seem to function as well as.hover()
which combines them together into one method.Also adding a
.stop()
will stop the current animation before starting a new one..stop(true, false)
will clear anything in the animation queue and not allow the current animation to complete.http://api.jquery.com/hover/
http://api.jquery.com/stop/