Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hover event fires twice (on enter and leave) in jQuery?

Tags:

jquery

hover

$(document).ready(function(){
    $("#menu a").hover(function(){
        $(this).animate({opacity: 0.25}, function(){
            $(this).animate({opacity: 1});
        });
    });
});

I applied this effect to my menu, so when the mouse goes over the link it shows this fade effect, but when the mouse is released from it, the effect plays again. It shouldnt. It should only play once when the mouse is over the link, not another time when the mouse is out.

like image 537
user1091856 Avatar asked Jan 05 '12 07:01

user1091856


3 Answers

.hover() has two callbacks, one for mouseenter and one for mouseleave.

You're probably better off with mouseenter:

$(document).ready(function(){
    $("#menu a").mouseenter(function() {
        $(this).animate({opacity: 0.25}, function(){
            $(this).animate({opacity: 1});
        });
    });
});
like image 123
Blender Avatar answered Nov 07 '22 02:11

Blender


try this with jQuery v1.7

$('#menu a').on({
    mouseover: function() {
        event.preventDefault();
        $(this).animate({opacity: 0.25});
    },
    mouseout: function() {
        event.preventDefault();
        $(this).animate({opacity: 1});
    }
});

working DEMO

like image 5
diEcho Avatar answered Nov 07 '22 01:11

diEcho


try this. add $(this).stop() on hoverout event

   $(document).ready(function () {
            $("#menu a").hover(function () {
                $(this).animate({ opacity: 0.25 }, function () {
                    $(this).animate({ opacity: 1 });
                });
            }, function () { $(this).stop(); });
        });
like image 2
Shoaib Shaikh Avatar answered Nov 07 '22 02:11

Shoaib Shaikh