2010-05-23 35 views
0

我有这个代码在IE中完美拖动东西 - 但是在Firefox中,对象的onmousedown拖动不会立即拖动,而是显示无入口光标,然后在onmouseup对象后自由拖动。该对象确实停止在下一个onmouseup上播放。该对象只应在onmousdown状态下拖动,而onmousup调用应通过使j_OK = 0来取消拖动。我想这可能是与里面的图像...对象拖动延迟问题

对象:

<em style=position:absolute;left:0;top:0;width:32;height:32;display:block> 
< img src=abc.gif onmousedown=P_MV(this.parentNode) style=position:absolute;left:0;top:0;width:inherit> 
</em> 



    function P_MV(t) 
{ 
p_E=t 
j_oy=parseInt(p_E.style.top) 
j_ox=parseInt(p_E.style.left) 
j_OK=1 
document.onselectstart=function(){return false} 
document.onmousemove=P_MVy 
} 

function P_MVy(e) 
{ 
if(j_OK) 
{ 
    p_E.style.top=(j_FF?e.clientY:event.clientY)-j_y+j_oy 
    p_E.style.left=(j_FF?e.clientX:event.clientX)-j_x+j_ox 
} 
return false 
} 
+0

代码看起来像它出来的obfuscater ...顺便说一下,它的良好的业务守则,坚持语句末,一个分号,即使JavaScript不要求它[它会让你的代码看起来更干净] ...另外,当在代码中定义一个html元素的属性时,请在其周围添加一段引号,以保持可读性> __ – Warty 2010-05-23 23:05:24

+0

j_y和j_x从未在代码中定义过?顺便说一句,P_MV让我想到p = mv ... [势头] – Warty 2010-05-23 23:13:41

+0

@ItzWarty:不仅添加分号让你的代码看起来更干净,而且在缩小代码时不会让代码变得混乱。 – Robusto 2010-05-23 23:57:07

回答

0

你不能只推荐用户使用clientX和clientY。这些属性在IE和其他浏览器中的工作方式不同。由于quirksMode says,只是为了获得正确的位置还有很多事情要做。下面是他的解决方案:

function doSomething(e) { 
    var posx = 0; 
    var posy = 0; 
    if (!e) var e = window.event; 
    if (e.pageX || e.pageY)  { 
     posx = e.pageX; 
     posy = e.pageY; 
    } 
    else if (e.clientX || e.clientY) { 
     posx = e.clientX + document.body.scrollLeft 
      + document.documentElement.scrollLeft; 
     posy = e.clientY + document.body.scrollTop 
      + document.documentElement.scrollTop; 
    } 
    // posx and posy contain the mouse position relative to the document 
    // Do something with this information 
} 
+0

这回答了cleint XY的问题,但并未提出问题...即使插入此代码,问题仍然存在。 – 2010-05-25 10:43:26