use*_*519 5 viewport mouseleave
我想检测鼠标何时离开顶部的视口(可以说是向北)。我在网上搜索并提出了如何检测鼠标何时离开窗口?. 这是一个好的开始,但它也会检测鼠标何时离开其他方向。我怎么能只检测到顶部的出口?
谢谢!
为了在不考虑滚动条和自动完成字段的情况下检测 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)