代码如下:

<!doctype html>

<html>

<head>

  <meta charset="utf-8">

  <title>获取鼠标在Canvas中的坐标位置</title>

  <style>

    #canvas{

      border:1px solid #ccc;

      width:300px;

      height:300px;

      overflow:hidden;

    }

  </style>

  <script>

    function get_canvas(ev,obj){

        m_clientX = ev.clientX-obj.offsetLeft;

        m_clientY = ev.clientY-obj.offsetTop;

        document.getElementById("tips").innerHTML = "当前坐标:X:"+ m_clientX + " ,Y:" +m_clientY;

    }

  </script>

</head>

<body>

  <div id="tips"></div>

  <div id="canvas" onmousemove="get_canvas(event,this)"></div>

</body>

</html>

兼容IE8+

posted on 2017-10-13 22:18  幽幽冷瞳  阅读(6815)  评论(0编辑  收藏  举报