检测鼠标何时离开视口顶部?

use*_*519 5 viewport mouseleave

我想检测鼠标何时离开顶部的视口(可以说是向北)。我在网上搜索并提出了如何检测鼠标何时离开窗口?. 这是一个好的开始,但它也会检测鼠标何时离开其他方向。我怎么能只检测到顶部的出口?

谢谢!

Dap*_*que 5

为了在不考虑滚动条和自动完成字段的情况下检测 mouseleave :

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");

  }
});
Run Code Online (Sandbox Code Playgroud)

然后你只需要删除条件:

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
Run Code Online (Sandbox Code Playgroud)


bpe*_*due 3

使用此插件: https: //github.com/carlsednaoui/ouibounce

当鼠标移出顶部视口时,它会触发一个事件。

在此输入图像描述