Why can't I reliably capture a mouseout event?

后端 未结 6 1107
日久生厌
日久生厌 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: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.

提交回复
热议问题