我想检测鼠标何时离开视口顶部(向北可以这么说)。我在网上搜索了How can I detect when the mouse leaves the window?。是一个好的开始,但它也可以检测鼠标何时离开其他方向。我怎么才能检测出顶部的出口?
谢谢!
答案 0 :(得分:3)
答案 1 :(得分:0)
为了检测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");
}
});
然后你只需要删除条件:
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