How do I check if the mouse is over an element in jQuery?

前端 未结 24 2110
不思量自难忘°
不思量自难忘° 2020-11-22 08:10

Is there a quick & easy way to do this in jQuery that I\'m missing?

I don\'t want to use the mouseover event because I\'m already using it for something else. I

相关标签:
24条回答
  • 2020-11-22 08:50

    I couldn't use any of the suggestions above.
    Why I prefer my solution?
    This method checks if mouse is over an element at any time chosen by You.
    Mouseenter and :hover are cool, but mouseenter triggers only if you move the mouse, not when element moves under the mouse.
    :hover is pretty sweet but ... IE

    So I do this:

    No 1. store mouse x, y position every time it's moved when you need to,
    No 2. check if mouse is over any of elements that match the query do stuff ... like trigger a mouseenter event

    // define mouse x, y variables so they are traced all the time
    var mx = 0; //  mouse X position
    var my = 0; //  mouse Y position
    
    // update mouse x, y coordinates every time user moves the mouse
    $(document).mousemove(function(e){
        mx = e.pageX;
        my = e.pageY;
    });
    
    // check is mouse is over an element at any time You need (wrap it in function if You need to)
    $("#my_element").each(function(){
        boxX = $(this).offset().left;
        boxY = $(this).offset().top;
        boxW = $(this).innerWidth();
        boxH = $(this).innerHeight();
        if ((boxX <= mx) &&
            (boxX + 1000 >= mx) &&
            (boxY <= my) &&
            (boxY + boxH >= my))
        {
            // mouse is over it so you can for example trigger a mouseenter event
            $(this).trigger("mouseenter");
        }
    });
    
    0 讨论(0)
  • 2020-11-22 08:55

    This would be the easiest way of doing it!

      function(oi) 
      {
       if(!$(oi).is(':hover')){$(oi).fadeOut(100);}
      }
    
    0 讨论(0)
  • 2020-11-22 08:57

    Set a timeout on the mouseout to fadeout and store the return value to data in the object. Then onmouseover, cancel the timeout if there is a value in the data.

    Remove the data on callback of the fadeout.

    It is actually less expensive to use mouseenter/mouseleave because they do not fire for the menu when children mouseover/mouseout fire.

    0 讨论(0)
  • 2020-11-22 08:58

    In jQuery you can use .is(':hover'), so

    function IsMouseOver(oi)
    {
       return $(oi).is(':hover');
    }
    

    would now be the most concise way to provide the function requested in the OP.

    Note: The above does not work in IE8 or lower

    As less succinct alternative that does work in IE8 (if I can trust IE9's IE8 modus), and does so without triggering $(...).hover(...) all over the place, nor requires knowing a selector for the element (in which case Ivo's answer is easier):

    function IsMouseOver(oi)
    {
        return oi.length && 
               oi.parent()
                 .find(':hover')
                 .filter(function(s){return oi[0]==this})
                 .length > 0;
    }
    
    0 讨论(0)
  • 2020-11-22 08:58

    Extending on what 'Happytime harry' said, be sure to use the .data() jquery function to store the timeout id. This is so that you can retrieve the timeout id very easily when the 'mouseenter' is triggered on that same element later, allowing you to eliminate the trigger for your tooltip to disappear.

    0 讨论(0)
  • 2020-11-22 09:01

    A clean and elegant hover check:

    if ($('#element:hover').length != 0) {
        // do something ;)
    }
    
    0 讨论(0)
提交回复
热议问题