-->

jquery mouseleave issue when moving too slow

2019-02-26 20:55发布

问题:

I am using the jQuery mouseenter and mouseleave events to slide a div down and up.

Everything works well except for the mouseleave which doesn't appear to fire ONLY if the mouse of moved off of the div quite slowly. If i move the mouse at a relatively normal or fast speed then it works as expected.

Can anyone explain this or provide any info on how to get around this?

Code:

$(document).ready(function() {
    $('header').mouseenter(function() {
        $(this).stop().animate({'top' : '25px'}, 500, function() {
            $(this).delay(600).animate({'top' : '-50px'}, 500);
        });
    }).mouseleave(function(e) {
        var position = $(this).position();
        if (e.pageY > position.top + $(this).height()) {
            $(this).stop().delay(600).animate({'top' : '-75px'}, 500) ;
        }
    });
});

回答1:

Try to use hover instead mouseenter and mouseleave;

$(document).ready(function() {
    $("#div").hover(function() {
         $("#something").slideDown(400);
    }, function() {
         $("#something").slideUp(400);
    });
});