How do you grab the mouse in the window?

THE QUESTION ALREADY HAS AN ANSWER (if you can provide a code that works instead of googling my question and a link to any answer that is suitable for sweet sweet internet points, then I will accept it as but since I already had all these questions, not pretend they work if you cannot provide the actual code that you tested)

If you just do

$(window).on("mouseout", function() { alert("OUT"); }); 

You can click on the left or right side of the window and not fire the event. I made jsfiddle here , but actually it works fine because there is a border around the iframe.

So what is the best way to find out if the mouse has left the window? I can put a 1px border around the page (consisting of 4 divs). I can control x / y and tell if the mouse is on the edge. But ideally, $ (window) .on ("mouseout", foo) will tell me when the mouse left the window.

+5
source share
1 answer

This answer works: fooobar.com/questions/33625 / ...

Using jQuery, you can rewrite the code as follows:

 $(window).on("mouseout", function(e) { if(!e.relatedTarget || e.relatedTarget.nodeName == 'HTML') { alert('left window'); } }); 

The code has been tested on Lightfox Firefox and Chrome.

0
source

Source: https://habr.com/ru/post/1233166/


All Articles