[英]How can I detect whether the mouse is in a certain part of the screen?
因此,我试图基于指针的Y坐标更改变量的值,但不确定如何执行此操作。 我尝试了这个:
var righttext = mousemove.clientY;
switch(righttext){
case //not sure what to put here
}
但我不确定该怎么办。 我想做的就是这样的例子: case "top 20% of screen"
。 我如何确定屏幕的坐标是什么,然后确定如何在switch语句中使用它?
我想影响该值的部分仅是屏幕水平分割的五分之一。
编辑:
因此,我链接到另一篇文章,并找到了与我所寻找内容相似的答案。 我将代码更改为如下所示:
document.addEventListener('mousemove', _.throttle(mouseMoveEventAction, 200)); function mouseMoveEventAction(e) { changetext(isInsideThreshold(e.clientY)); } var rpaneltext = ""; function changetext(isActive) { if (isActive) { rpaneltext = "awareness"; } else { rpaneltext = ""; } } function isInsideThreshold(cursorY) { var threshold = .2; var clientHeight = document.documentElement.clientHeight; return cursorY > (clientHeight - threshold); }
在这里的项目:会受到righttext
影响的righttext
是document.querySelector(".r2").textContent = righttext
您可以查看window.innerHeight
和window.innerWidth
。 它们返回视口尺寸。
这样的if-else逻辑可能会更好。
if (event.clientY < window.innerHeight / 2) {
// it's in the upper half
} else {
// it's in the lower half
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.