如何连接 onmousemove 和 onmousedown?

How to connect onmousemove with onmousedown?

我想按下鼠标按钮并移动光标,按下按钮时显示光标的坐标。当我停止点击时,它应该停止显示坐标。

代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Example</title>
  <style>
    body {
      height: 3000px;
    }
  </style>
</head>
<body>
<script>
  (function() {
    "use strict";

    document.onmousedown = handleMouseDown;
    function handleMouseDown(event) {
      console.log("down")
    }

    document.onmousemove = handleMouseMove;
    function handleMouseMove(event) {
      var dot, eventDoc, doc, body, pageX, pageY;

      event = event || window.event;
      if (event.pageX == null && event.clientX != null) {
        eventDoc = (event.target && event.target.ownerDocument) || document;
        doc = eventDoc.documentElement;
        body = eventDoc.body;

        event.pageX = event.clientX +
          (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
          (doc && doc.clientLeft || body && body.clientLeft || 0);
        event.pageY = event.clientY +
          (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
          (doc && doc.clientTop  || body && body.clientTop  || 0 );
      }
      console.log("left: " + event.pageX + "px -- right: " + event.pageY + "px");
    }
  })();
</script>
</body>
</html>

提前致谢。

只是 attach/detach mousemove 处理 mousedown/mouseup 事件的函数:

document.onmousedown = function () {
    document.onmousemove = handleMouseMove;
};
document.onmouseup = function () {
    document.onmousemove = null;
};

function handleMouseMove(event) {
  var dot, eventDoc, doc, body, pageX, pageY;

  event = event || window.event;

  if (event.pageX == null && event.clientX != null) {
    eventDoc = (event.target && event.target.ownerDocument) || document;
    doc = eventDoc.documentElement;
    body = eventDoc.body;

    event.pageX = event.clientX +
      (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
      (doc && doc.clientLeft || body && body.clientLeft || 0);
    event.pageY = event.clientY +
      (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
      (doc && doc.clientTop  || body && body.clientTop  || 0 );
  }
  console.log("left: " + event.pageX + "px -- right: " + event.pageY + "px");
}