You are looking for the stop function, followed by show (or hide or css ), depending on what state you want opacity in the end).
function hoverOpacity() { $('#fruit').mouseover(function() { $(this).stop(true).animate({opacity: 0.5}, 1500); }); $('#fruit').mouseout(function() { $(this).stop(true).animate({opacity: 1}, 1500); }); }
true tells the animation to go to the end. If this is the only element animation, this should be fine; otherwise, as I said, you can look at css to explicitly set the desired opacity.
Separately, you can use mouseenter and mouseleave rather than mouseover and mouseout , for two reasons: 1. mouseover repeated when the mouse moves over an element and 2. Both bubbles are mouseover and mouseout , and therefore, if your "fruit" element has children, You will also receive events from them that destabilize this type of animation.
source share