Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent mouse enter/leave to occur too many times

I have a piece of code that basically says: if you roll over this , then the other thing appears and if you roll out then it will disappear.

The problem is that if I take the mouse and roll over/out too many times then the elements appears/disappears too many times (because I have created a lot of events for it by mistake)

my code looks like this:

$('div.accordionContent').mouseenter(function()
{
    $(this).find(".something").animate({left: 0}, 300)}).mouseleave(function() {
    $(this).find(".something").animate({
    left: -200}, 500);;
}); 

How do I tell it to avoid multiple hovering?

I use jQuery 1.4.3 if that helps..

like image 998
Alon Avatar asked Dec 12 '11 08:12

Alon


People also ask

Is a mouse event that will be triggered when the mouse leaves an object?

The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children.

Which event is triggered when mouse enters a specific region?

The mouseenter event is fired at an Element when a pointing device (usually a mouse) is initially moved so that its hotspot is within the element at which the event was fired.

What is mouse leave event?

The mouseleave event occurs when the mouse pointer leaves the selected element. The mouseleave() method triggers the mouseleave event, or attaches a function to run when a mouseleave event occurs. Note: Unlike the mouseout event, the mouseleave event only triggers when the mouse pointer leaves the selected elements.

What is mouse enter event?

The onmouseenter event occurs when the mouse pointer is moved onto an element. Tip: This event is often used together with the onmouseleave event, which occurs when the mouse pointer is moved out of an element. Tip: The onmouseenter event is similar to the onmouseover event.


1 Answers

Rather than avoiding multiple triggering, try stopping the animation before starting another.

$('div.accordionContent').mouseenter(function() {
    $(this).find(".something").stop().animate(...)
}); 
like image 135
Petah Avatar answered Oct 10 '22 05:10

Petah