无需点击即可使用坐标

因此,一旦使用用户在页面上的任意位置单击鼠标右键,我一直在尝试呈现div而不是上下文菜单,为此,我需要接收单击的坐标。目前,我正在这样做

function printMousePos(event) {
  let coordinates = [event.clientX, event.clientY];
  //console.log(coordinates);
  return coordinates;
}

document.addEventListener("click", printMousePos);

在控制台中,我得到了一个包含x和y的数组,但是无法在函数外部使用它们。我一直在尝试类似

let a = function (event){...}

but it doesn't seem to return the array in any case, so how could I refer to x and y? The problem is that those are dynamic and change only when the event occurs. Should I just render the menu inside of the printMousePos(event) ? Or is there any other way to get x and y?

评论