事件和光标位置

时间:2010-11-06 11:09:36

标签: javascript html

我想在光标在红色div和鼠标按钮上方时显示鼠标坐标。

问题是当我释放div外部的按钮并将光标返回到div时,onmousemove事件不会被删除。

注意:我不想使用像jQuery这样的库。

            

        <script type="text/javascript">

              window.onload = function(){

      document.getElementById("thediv").onmousedown = AttachEvent;

     }
            function GetPos(e){                                
                document.getElementById('X').value = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                document.getElementById('Y').value = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

            }

            function UnaatachEvent(e){
                document.getElementById("thediv").onmousemove = null;
                document.getElementById("thediv").onmouseup = null;
            }

            function AttachEvent(e){
                document.getElementById("thediv").onmousemove = GetPos;
                document.getElementById("thediv").onmouseup = UnaatachEvent;
            }
        </script>
    </head>
    <body>
        <input type="text" id="X" size="3">X-position
        <br/>
        <br/>
        <input type="text" id="Y" size="3">Y-position 
        <div style="width:100px;height:100px;background-color:red;margin:auto;" id="thediv">
        </div>
    </body>

更新
使用jQuery非常简单:(仅在Firefox中测试) link text

$(document).ready(function(){

    $('#thediv').mousedown(attachEvent);
  });  
function attachEvent(e){
       $('#thediv').mousemove(getPos).mouseup(unattachEvent);

      return false;
}

function getPos(e){

  document.getElementById('X').value = e.pageX;
  document.getElementById('Y').value = e.pageY;            
 return false;
}

function unattachEvent(e){
   $('#thediv').unbind("mousemove", getPos).unbind("mouseup", unattachEvent);

请注意主要情景:

  1. 如果在广场外点击按钮,并在按住时移动到suqre - 不要更新位置。
  2. 如果在广场内点击按钮,并在按住时向外移动 - 停止更新,将其恢复(不释放按钮)继续更新
  3. 换句话说,初始moused应该在正方形上才开始更新。

    我想用javascript做同样的事情。

1 个答案:

答案 0 :(得分:3)

您的代码可以更好地编写为( Working Demo

var div = document.getElementById("thediv");
div.onmousedown = attachEvent;

// if mouse released, stop watching mouseover
document.onmouseup = function () {
  div.onmouseover = null;
}

function attachEvent() {
  this.onmousemove = getPos;
  this.onmouseout  = unattachEvent;
  this.onmouseup   = unattachEvent;
  return false; // prevent Firefox "drag behavior"
}

function unattachEvent() {
  this.onmousemove = null;
  this.onmouseup   = null;
  this.onmouseout  = null;
  // wait for the mouse to come back
  this.onmouseover = attachEvent;
}

function getPos(e) {
  e = e || window.event;
  var docEl = document.documentElement;
  var scrollLeft = docEl.scrollLeft || document.body.scrollLeft;
  var scrollTop  = docEl.scrollTop || document.body.scrollTop;
  document.getElementById('X').value = e.pageX || (e.clientX  + scrollLeft);
  document.getElementById('Y').value = e.pageY || (e.clientY  + scrollTop);
}​