简体   繁体   English

Mouseout和Mouseenter jQuery函数

[英]Mouseout and Mouseenter jquery function

I used the jQuery mouseout and mouseenter function. 我使用了jQuery mouseout和mouseenter函数。 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? 我该如何解决我的问题?

I added in .stop() just before the animation which will stop the animation in place and should stop the jumping. 我在动画之前添加了.stop() ,它将在适当位置停止动画并应停止跳跃。

$('.controlNav li').mouseover(function() {
    $('.hover', this).css({ display: 'block' }).stop().animate({ top: -73, opacity: 1 }, 450, 'swing' );
}).mouseout(function(){
    $('.hover', this).css({ display: 'none' }).stop().animate({ top: -60, opacity: 0 });
});

The problem originaly is not mouseout or mouseover events. 最初的问题不是出mouseoutmouseover事件。 They are working as they should to work. 他们正在努力工作。 It means even if you mouse over the element for just 1ms it will work. 这意味着即使您将鼠标悬停在元素上仅1ms它也将起作用。

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. 您可以手动执行此操作,也可以只使用jQuery悬浮意图插件实现该非常好用的操作。

It's better to not use mouseout or mouseover event and use jQuery .hover() (if you are using the plug in .hoverIntent() for more clean and readable code. 最好不要使用mouseoutmouseover事件,而应使用jQuery .hover() (如果您使用.hoverIntent()的插件获取更清晰易读的代码)。

.mouseover() and .mouseout() give strange results because mouseover() fires more than once while your mouse is still inside the element. .mouseover().mouseout()给出奇怪的结果,因为当鼠标仍位于元素内时, mouseover()会触发多次。 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. .mouseenter().mouseleave()更好,因为它们只能在进入或离开元素时触发一次。 However, they still do not seem to function as well as .hover() which combines them together into one method. 但是,它们似乎仍不如.hover()那样好,后者将它们组合为一个方法。

Also adding a .stop() will stop the current animation before starting a new one. 另外,添加.stop()将在开始新动画之前停止当前动画。 .stop(true, false) will clear anything in the animation queue and not allow the current animation to complete. .stop(true, false)将清除动画队列中的所有内容,并且不允许当前动画完成。

$('.controlNav li').hover(
    function() {
        $('.hover', this).css({ display: 'block' }).stop(true, false).animate({ top: -73, opacity: 1 }, 450, 'swing' );
},
    function() {
        $('.hover', this).css({ display: 'none' }).stop(true, false).animate({ top: -60, opacity: 0 });
});

http://api.jquery.com/hover/ http://api.jquery.com/hover/

http://api.jquery.com/stop/ http://api.jquery.com/stop/

set some variable as mutex, like: 将一些变量设置为互斥体,例如:

var isActive = false;
('.hover').css('opacity', 1);
    $('.controlNav li').mouseover(function() {
        if(isActive) return false;
        isActivce = true;
        $('.hover', this).css({ display: 'block' }).animate({ top: -73, opacity: 1, complete: function(){isActive = false} }, 450, 'swing' );
    }).mouseout(function(){
        $('.hover', this).css({ display: 'none' }).animate({ top: -60, opacity: 0 });
    });

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

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