ivb*_*ivb 5 javascript browser mouse-position
在浏览器中,我试图确定代表鼠标位置的点是否在我称之为"外部区域"的位置.例如,在附加的图像中,外部区域是具有蓝色背景的区域.
替代文字http://i34.tinypic.com/r8grwz.png
在代码和图像中,W代表浏览器视口的宽度,而H代表高度,而x代表鼠标位置
现在,我正在使用这段代码来做到这一点:
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")
Run Code Online (Sandbox Code Playgroud)
虽然它按原样工作,但我想知道是否有更好的方法呢?
您不需要检查它是否大于0且小于W,因为指针x位置不能小于0或大于W.这同样适用于Y轴.以下应该足够了:
if((x>w2 || x<w1) || (y>h2 || y<h1)){
console.log("We are in the outer area");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
940 次 |
| 最近记录: |