0

I am having a piece of code which checks if the mouse moves out of the browser window from the top of the browser.

For that i am using addEventListener function with mouseout event.

function addEvent(obj, evt, fn) {
        obj.addEventListener(evt, fn, false);
    }
}
addEvent(window, "mouseout", function(e) {
    if (e.clientY > 0) {
      return;
    }
    alert('window out from top');  
    }
});

This works as expected on Chrome, Firefox etc. But for IE, Edge e.clientY never becomes less than 0 if I move the cursor out of the window very fast. It works fine if i move the cursor really slow.

user966123
  • 631
  • 1
  • 9
  • 18
  • Every browser that has come out since IE 9 supports `addEventListener` and events passed to the event callback. Do you really need to still support IE8? – Scott Marcus Jul 29 '19 at 18:39
  • I am not supporting IE8 but the question is not that. I have addEventListener but it does not capture clientX and clientY positions correctly when I move mouse out of the window very fast – user966123 Jul 29 '19 at 18:42
  • I understand that, but you are showing a dedicated wrapper function to attach event handlers and if you aren't supporting IE8, it is completely unnecessary, nor is the `e = e ? e : window.event;` line. – Scott Marcus Jul 29 '19 at 18:44
  • ok, i have removed those lines now. – user966123 Jul 29 '19 at 18:45
  • You don't need the `addEvent` function at all. All you need is: `document.addEventListener("mouseout", function(e) {if(e.clilentY < 0) { alert("Out from top")}});` – Scott Marcus Jul 29 '19 at 18:48
  • so this is just a part of the code that pasted here. I have other functions which add other event listeners. Also, i do not see how that would solve the issue I am seeing. – user966123 Jul 29 '19 at 18:53
  • I understand that it doesn't directly address your question, which is why I'm posting a comment, rather than an answer. – Scott Marcus Jul 29 '19 at 19:12
  • I made a test with your code and reproduced the issue on my side. This might be a bug of IE and Edge. I will try to submit the feedback regarding this issue. As a temporary workaround, you could use the solution in [this answer](https://stackoverflow.com/questions/923299/how-can-i-detect-when-the-mouse-leaves-the-window/3187524#3187524) to detect if the mouse is out of browser. Thanks for your understanding. – Yu Zhou Jul 30 '19 at 05:54

0 Answers0