I want to detect when the mouse leaves the viewport on top (to the north so to say). I searched the net and came up with How can I detect when the mouse leaves the window?. Is a good start, but it also detects when the mouse leaves to other directions. How could I only detect the exit on top?
Thanks!
In order to detect mouseleave without taking in account the scroll bar and the autcomplete field :
document.addEventListener("mouseleave", function(event){
if(event.clientY <= 0 || event.clientX <= 0 || (event.clientX >= window.innerWidth || event.clientY >= window.innerHeight))
{
console.log("I'm out");
}
});
Then you just have to delete conditions :
event.clientY <= 0 is when the mouse leave from the top
event.clientX <= 0 is when the mouse leave from the left
event.clientX >= window.innerWidth is when the mouse leave from the right
event.clientY >= window.innerHeight is when the mouse leave from the bottom
Use this plugin: https://github.com/carlsednaoui/ouibounce
It fires an event when the mouse moves out of the top viewport.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With