25 lut 2009

Współrzędne myszki, pozycja kursora myszki Javascript

<html>
  <head>
    <script type="text/javascript">
      var IE = document.all ? true : false;
      if (!IE) document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove = myMouseXY;
      var pX = 0;
      var pY = 0;
      function myMouseXY(e) {
        if (IE) {
          pX = event.clientX + document.body.scrollLeft;
          pY = event.clientY + document.body.scrollTop;
        } else {
          pX = e.pageX;
          pY = e.pageY;
        }
        if (pX < 0) pX = 0;
        if (pY < 0) pY = 0;

        document.getElementById("ws").value = pX + ',' + pY;
        document.getElementById("dymek").style.left = pX;
        document.getElementById("dymek").style.top = pY;
        return true;
      }
    </script>
  </head>
  <body>
    <div id="dymek" style="position: absolute;">
      <input type="text" id="ws">
    </div>
  </body>
</html>