views:

1626

answers:

5

i have a table with some columns. in each of them is a picture where i have a onmouseover onmouseout event on it, which show a message in a div and hide the msg.

my problem is - after a user goes quick from left to right (and moving) over a lot o images. all mouseover/out events of the images where executed, which looks stupid... is it possible to rearrange the internal event stack to avoid this? so that he executes only the current (mostly the first event) -> and than the last one, if it is not same type eg. if mouseover over first image is executed and mouse moving position stops over an image 3times next the first one. i can avoid all other events firing, because the mouse stopped over an image and the mouseover is like the one where i stopped with the mouse.

how can i avoid this multiple event firing?!

+2  A: 

You need to check out the hoverIntent plugin, which addresses this problem.

karim79
+2  A: 

We've had the exact same problem, What we've done is on the mouseover event is to set a variable _mouseOn to true (set to false on mouseout) then set a oneTime event over that fires in say 500 ms.. The one time event will check if the _mouseOn is true and display the image

    function Hover() {
  _mouseOn = true;
  $(document).oneTime(500, "500ms", functionToCheckTheMouseOnAndDisplayTheImage);
};
TWith2Sugars
but how your functionToCheckTheMouseOnAndDisplayTheImage() knows where the mouse is right now (which image has to be shown?!?!)
stephan
Simple, if the mouse is not on the image it will fire the mouseout event which will set _mouseOn to false and the image will not be shown.
TWith2Sugars
don't understand how it works :-/
stephan
You attach the "Hover" function to the to the elements you want to hover over. When the move your mouse in to the element the "Hover" function is fired, this will set off a timer, in when the timer has elapsed it will call the "functionToCheckTheMouseOnAndDisplayTheImage". If the mouse is still in the element then the image will pop up in what ever code you use. If the mouse is moved out of the element before the image is shown it will set the _MouseOn variable to false, which means when the "functionToChe..." is called(for that element) it will not display the image. Hope that clears it up :)
TWith2Sugars
stephan
the oneTime function will fire off a separate timer, you every time you mouse over an element a brand new timer is fired - if you mouse over several, then several timers are fired. Hope this explains it a bit better
TWith2Sugars
+1  A: 

You can't, and shouldn't try to, avoid the events firing. What you should avoid is your code immediately responding to them by doing something that winds up looking stupid. For example, you can have your mouseovers register, with some controller object, which image the user is currently over, and set a short timeout to the function that triggers the actual behavior (removing a previous timeout if it's already running). The mouseout unregisters the image and removes the timeout. That way, when the behavior runs, you only operate on the image that the user moused over most recently.

chaos
+1  A: 
//Global timeout handle for mouseover and mouseout
var timeoutHandle;

$(document).ready(function() {

    BindMouseHover($(".helptext")); 

});//closing ready

//bind mouseover and mouseout actions on all elements 
function BindMouseHover(elements) {            
    $(elements).hover(
        function() {
            timeoutHandle = setTimeout('HandleMouseHover(true)', 1000);
        },
        function() {
            HandleMouseHover(false);
        }
    );
}

//Handle Mouseover and mouseout events
function HandleMouseHover(bDelay) {
    if (bDelay) {                
        $(".tooltip").show();
    }
    else {
        $(".tooltip").hide();
        clearTimeout(timeoutHandle);
    }
}

Explanation: On every mouseover schedule a call to DelayedTooltip(true) after 1000ms and save the setTimeout handle to timeoutHandle

If mouseout happens within that 1000ms interval then simply call clearTimeout(timeoutHandle) to cancel the setTimeout

This can be easily extended to apply to many heterogeneous elements and wire the customize tooltip text based on the element hovered.

Click here to know more about JavaScript Timing Events.

TigerAmit
A: 

I think it's better (from http://bavotasan.com/demos/fadehover/, THANKS)

$(document).ready(function(){ $(".a").hover( function() { $(this).stop().animate({"opacity": "0"}, "slow"); }, function() { $(this).stop().animate({"opacity": "1"}, "slow"); }); });
sekkkmo