如何检测鼠标是否在屏幕的某个部分?

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 语句中使用它?

我想影响值的部分只是水平划分的屏幕的五分之一。

编辑:

所以我被链接到另一个 post,并找到了与我正在寻找的内容相似的答案。我将代码更改为如下所示:

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);
}
但它仍然不起作用。有什么想法吗?

项目here:受righttext影响的部分是document.querySelector(".r2").textContent = righttext

你可以看看window.innerHeightwindow.innerWidth。他们 return 视口尺寸。

像这样的 if-else 逻辑可能会让你更幸运。

if (event.clientY < window.innerHeight / 2) {
    // it's in the upper half
} else {
    // it's in the lower half
}