【问题标题】:need to capture mouse position in javascript需要在javascript中捕获鼠标位置
【发布时间】:2012-01-11 06:11:56
【问题描述】:

如何使用 javascript 和画布捕捉鼠标的位置?

当我进入这个页面时:http://billmill.org/static/canvastutorial/mouse.html

他们展示了这个:

function init_mouse() {
  canvasMinX = $("#canvas").offset().left;
  canvasMaxX = canvasMinX + WIDTH;
}

function onMouseMove(evt) {
  if (evt.pageX > canvasMinX && evt.pageX < canvasMaxX) { //how can you access the canvasMinX when its out of scope?
//also, what is pageX? is it the coordinate of the mouse? if not, how do i get it?
    paddlex = evt.pageX - canvasMinX;
  }
}

$(document).mousemove(onMouseMove);

最后,我需要在鼠标点击时发生这种情况。所以我这样做:

$(document).mouseclick(onMouseClick)

是吗?

【问题讨论】:

    标签: javascript ajax events dom


    【解决方案1】:

    这是关于如何使用 JS 实现它的最佳解释:JavaScript Capture Mouse X-Y Position Script - Quick-Take Mini-Tutorial

    【讨论】:

      【解决方案2】:

      试试这个...希望它有所帮助。

          $("#divClick").click(function (e) {
                  var posX = $(this).position().left;
                  var posY = $(this).position().top;
                  var cursorX = (e.pageX - posX);
                  var cursorY = (e.pageY - posY);
                  //cursorX, cursorY is the absolute position of the mouse pointer
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多