确定鼠标位置是否靠近浏览器边缘

发布于 2024-08-07 07:49:34 字数 564 浏览 4 评论 0原文

在浏览器中,我试图确定代表鼠标位置的点是否位于我所说的“外部区域”。例如,在附图中,外部区域是蓝色背景的区域。

替代文本

在代码和图像中,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.

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾`听者〃 2024-08-14 07:49:34

您不需要检查它是否大于 0 且小于 W,因为指针 x 位置不能小于 0 或大于 W。这同样适用于 Y 轴。以下内容应该足够了:

if((x>w2 || x<w1) || (y>h2 || y<h1)){
  console.log("We are in the outer area");
}

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:

if((x>w2 || x<w1) || (y>h2 || y<h1)){
  console.log("We are in the outer area");
}
余罪 2024-08-14 07:49:34

您可以将整个页面包装到 DIV(称为 outer)中,然后连接到 hover 事件。

You could wrap your whole page into a DIV (called outer) and then connect to the hover event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文