gpt4 book ai didi

viewport - 检测鼠标何时离开视口(viewport)顶部?

转载 作者:行者123 更新时间:2023-12-05 01:02:32 26 4
gpt4 key购买 nike

我想检测鼠标何时离开顶部的视口(viewport)(可以说是向北)。我在网上搜了下How can I detect when the mouse leaves the window? .是一个好的开始,但它也会检测鼠标何时离开其他方向。我怎么能只检测到顶部的导出?

谢谢!

最佳答案

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

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

关于viewport - 检测鼠标何时离开视口(viewport)顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com