How do I avoid massive onmouseover onmouseout firing?

前端 未结 5 1227
轻奢々
轻奢々 2021-01-06 19:55

I have a table with some columns. In each of them is a picture where I have an onmouseover/onmouseout event on it, which shows a message in a div and hides the message.

相关标签:
5条回答
  • 2021-01-06 20:36

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

    0 讨论(0)
  • 2021-01-06 20:37

    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);
    };
    
    0 讨论(0)
  • 2021-01-06 20:39

    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.

    0 讨论(0)
  • 2021-01-06 20:57

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

    <script> 
    $(document).ready(function(){
        $(".a").hover(
        function() {
            $(this).stop().animate({"opacity": "0"}, "slow");
        },
        function() {
            $(this).stop().animate({"opacity": "1"}, "slow");
        });
    
    });
    </script> 
    
    0 讨论(0)
  • 2021-01-06 21:01
    //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.

    0 讨论(0)
提交回复
热议问题