javascript - Determining if a mouse position is near the edges of the browser - Stack Overflow

In a browser, I'm trying to determine if the point representing the mouse position is in what I ca

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?

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?

Share Improve this question edited May 1, 2019 at 1:05 Glorfindel 22.7k13 gold badges89 silver badges119 bronze badges asked Oct 15, 2009 at 8:51 ivbivb 1671 silver badge13 bronze badges 2
  • Can I ask you what you're going to make? Is it some kind of "scroll on edge" thing like in most RTS games? – Boris Callens Commented Oct 15, 2009 at 9:08
  • No. The page won't scroll with the mouse. It's more like I want to set up some data when the mouse is in the outer area. – ivb Commented Oct 15, 2009 at 9:12
Add a ment  | 

2 Answers 2

Reset to default 5

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");
}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742366377a4430394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信