确定鼠标位置是否靠近浏览器边缘
在浏览器中,我试图确定代表鼠标位置的点是否位于我所说的“外部区域”。例如,在附图中,外部区域是蓝色背景的区域。
在代码和图像中,W代表浏览器视口的宽度,而H代表高度,x,y代表鼠标位置
现在,我正在使用这段代码来做到这一点:
if (((x>0 && x<w1) || (x>w2 && x<W))
||
((x>w1 && x<w2) &&
((y>0 && y<h1) || (y>h2 && y<H))
))
console.log("we are in the outer area")
虽然它按原样工作,我想知道是否有更好的方法?
In a browser, I'm trying to determine if the point representing the mouse position is in what I call an "outer area" . For example, in the image attached, the outer area is the one with a blue background.
In the code and image W represents the width of the viewport of the browser, while H represents the height, and x,y for mouse position
Right now, I'm using this piece of code to do it:
if (((x>0 && x<w1) || (x>w2 && x<W))
||
((x>w1 && x<w2) &&
((y>0 && y<h1) || (y>h2 && y<H))
))
console.log("we are in the outer area")
While it works as it is, I'm wondering if is there any better way to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要检查它是否大于 0 且小于 W,因为指针 x 位置不能小于 0 或大于 W。这同样适用于 Y 轴。以下内容应该足够了:
You don't need to check if it is more than 0 and less than W, since the pointer x position can't be less than 0 or more than W. The same applies to the Y axis. The following should be enough:
您可以将整个页面包装到 DIV(称为
outer
)中,然后连接到hover
事件。You could wrap your whole page into a DIV (called
outer
) and then connect to thehover
event.