最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Events and cursor position - Stack Overflow

programmeradmin5浏览0评论

I want to show mouse coordinates while the cursor over the red div and mouse button is down.

The problem is when i release the button outside the div and return the cursor back to the div, onmousemove event is not removed.

NOTE: I don't want to use any library like 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>

UPDATE:
With jQuery it's very easy: (tested in Firefox only) 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);

Please note main scenarios:

  1. If button was clicked outside the square, and while holding, moved to suqre - Don't upddate position.
  2. If button was clicked inside the square , and while holding, move outside - stop update, bring it back (without releasing the button) continue update

In other words, the initial mousedown should be on square only to start the update.

I want to do the same with javascript.

I want to show mouse coordinates while the cursor over the red div and mouse button is down.

The problem is when i release the button outside the div and return the cursor back to the div, onmousemove event is not removed.

NOTE: I don't want to use any library like 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>

UPDATE:
With jQuery it's very easy: (tested in Firefox only) 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);

Please note main scenarios:

  1. If button was clicked outside the square, and while holding, moved to suqre - Don't upddate position.
  2. If button was clicked inside the square , and while holding, move outside - stop update, bring it back (without releasing the button) continue update

In other words, the initial mousedown should be on square only to start the update.

I want to do the same with javascript.

Share Improve this question edited Nov 7, 2010 at 21:51 jullin asked Nov 6, 2010 at 11:09 jullinjullin 6332 gold badges13 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Your code can be better written as (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 e 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);
}​
发布评论

评论列表(0)

  1. 暂无评论