日期:2014-05-16  浏览次数:20395 次

鼠标拖动,放开时坐标问题...
var div_img = document.getElementById("img");
   
  div_img.onmousedown = startdarg;
  div_img.onmouseup = stopdarg;

function startdarg(event)
{
  if (!event)
  {
  event = window.event;
  x=event.offsetX;
  y =event.offsetY;
  alert(x + " " + y);
   
  }

}

function stopdarg(event)
{
  if (!event)
  {
  //alert("haha");
  event = window.event;
  var a = event.offsetX;
  var b = event.offsetY;
   
  alert(a + " " + b);
   
  }
   
}

这样子的话,鼠标左键按下去,是可以得到正确的值,但是一直按着鼠标进行拖动,放开鼠标时,根本就不会执行stopdrag,要怎么才能得到鼠标放开时的坐标呢??


------解决方案--------------------
没有执行是因为你根本没有触发stopdrag,因为你startdrag的时候alert了一下,这个时候已经blur了,松开鼠标触发不到事件了
JScript code
<script>
var div_img = document.getElementById("img"); 
         
        div_img.onmousedown = startdarg; 
        div_img.onmouseup = stopdarg; 

function startdarg(event) 
{ 
        if (!event) 
        { 
        event = window.event; 
         x=event.offsetX; 
          y =event.offsetY; 
        //alert(x + "  " + y); 
          self.status=x+":"+y;       
        } 

} 

function stopdarg(event) 
{ 
        if (!event) 
        { 
                //alert("haha"); 
                event = window.event; 
                var a = event.offsetX; 
                var b = event.offsetY; 
                 
                //alert(a + "  " + b); 
                self.status=a+":"+b;
                 
        } 
         
}
</script>