Why can't I reliably capture a mouseout event?

后端 未结 6 1106
日久生厌
日久生厌 2020-11-30 00:18

I need to know when the mouse cursor leaves a div. So I hook up the mouseout event. However, if I move the mouse very quickly out of the div<

相关标签:
6条回答
  • 2020-11-30 00:50

    I found your question and the lack of other clear answers useful because it told me that I had to create a workaround. Which I did using the ideas presented in your question and the other contributors.

    I have same problem when I use jquery mouseleave elem.bind('mouseleave', data, mouseLeavesZone);

    The problem is intermittent and may be related to a busy CPU on the client. Say, for example, the CPU is busy elsewhere when your mouse moves out of a div. Then it seems logical that this could be the cause of the bug. I agree; this should be fixed by the browser vendors.

    See http://jsfiddle.net/bgil2012/gWP5x/1/

    (Aside: My JQuery code needs to use older jQuery methods because it has to run in Drupal 7 which is running jQuery 1.4, at this time and without applying patches that are coming).

    0 讨论(0)
  • 2020-11-30 00:56

    I ran into this problem a few times and I came to accept the issue as a fact of life. But depend on your needs, you can just use CSS as I did. For example, if I just want to show/hide an element base on another element got hovered, then CSS is the way to go. Here is a working, reliable example:

    .large {
      width: 175px; height: 175px;
      position: absolute;
      border-radius: 100%;
    
      /*hide the glass by default*/
      top: -9999px;
      left: -9999px;
      opacity: 0;
      transition: opacity .2s ease-in-out;
      z-index: 100;
      pointer-events: none;
    }
    
    .small:hover + .large {
      opacity: 1;
    }
    

    http://codepen.io/tanduong/pen/aBMxyd

    0 讨论(0)
  • 2020-11-30 00:57
    1. Why can't the browser can't reliably capture the mouseout event? If I can reliably tell when the mouse has left the div using the above workaround, why can't the browser do it?

      I think you answered this one yourself when you said:

      Compared to letting the browser do all this natively, performing calculations on every pixel move is a bit of a performance hit.

      The browser does not interpolate between the frames, thus, as you stated it would demand a lot more resources and memory, which may be why it isn't "fixed".

    2. If some pixel movements are missed just as the mouse crosses the boundary of the div, why does it follow that the mouseout event should also be skipped? When the browser finally starts registering the mouse's position again (after a sudden fast movement), even if the mouse is now miles outside the box, the point is that it used to be in the box and no longer is. So why doesn't it then fire the mouseout event then?

      I don't know for sure, but I don't think it's a condition of "it was in and now it's out". Instead, it's whether it crosses that boundary (if MouseX - ElemOffsetX= 1). I agree, it doesn't make as much sense, but it could be because if you set the value to > 1 it would trigger the event multiple times. Otherwise it would have to keep track of the events, which is not in JS nature, seeing how it just adds events asynch to a stack.


    What you could try is using jQuery's mouseleave event. This does two things, which delays the firing of the event:

    1. It traverses the DOM tree to see if it truly left the element
    2. I think it implements a timeout call, which should solve the interpolation problem that you noticed.
    0 讨论(0)
  • 2020-11-30 01:02

    I know that you don't want a workaround, but you don't need to check mouse's x/y to know if you are in or out an element. You could simply check the element from which the mousemove event was fired. If you put a mousemove on document, the event will fire from one of its children, and you can compare that element with your element to know if it is one of its descendants.

    Or you could go up the parentNode tree and stop if you find your element. Then you know you are inside the element and still in it, otherwise you reach the document and you are out.

    Some browsers implement the mouseenter/mouseleave events that, I've noticed, are more accurate than mouseout. Prototype and jQuery have a workaround for browsers that don't implement these new events. Mouseleave does not fire from an element's children, whereas mouseout does.

    0 讨论(0)
  • 2020-11-30 01:04

    Here's a simple workaround.

    In your onMouseOver listener, you can add a 'mousemove' listener to the window:

    <div onMouseOver={() => {
        setMouseOver(true)
    
        let checkMouseLeave = (e: MouseEvent) => {
            if (rootRef.current
                && !rootRef.current.contains(e.target as HTMLElement)) {
                setMouseOver(false)
                window.removeEventListener('mousemove', checkMouseLeave)
            }
        }
    
        window.addEventListener('mousemove', checkMouseLeave)
    }
    ></div>
    

    Then you can check on each mouse move until the mouse is outside of your div (rootRef.current in our example).

    0 讨论(0)
  • 2020-11-30 01:15

    You describe moving the mouse very quickly. When you stop, is the pointer still within the page? That is, is your mouse pointer still hovering over some part of the visible web page?

    If it has gone outside, then it's not necessarily clear what the browser should do. The mouseout event should have a relatedTarget property that targets what the mouse pointer has gone into. If the mouse pointer is already outside of the page area, there would be no related target to point to.

    Put another way, when the mouse leaves the page area, the browser stops tracking it and stops reporting its position. If you move your mouse fast enough, from the browser's perspective, the mouse simply disappeared. It's not until you bring the mouse back into the bounding box of the viewable page that the browser knows where it is, and then triggers all appropriate movement-based actions (like mouseout).

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