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

移动DIV的问题???
下面的DIV移动起来为什么很困难?
请问还有什么移动DIV的方法吗?

HTML code
<SCRIPT LANGUAGE="JavaScript">
<!--
var oldX=0,oldY=0;
var isDrag = false;
function init(){
    isDrag=true;
    oldX=window.event.x;
    oldY=window.event.y;
}

function end(){
    isDrag=false;
}

function moveIt(obj){
    if(isDrag){
    var x =window.event.x;
    var y = window.event.y;
    obj.style.left = parseInt(obj.style.left.replace("px","")) + (x - oldX);
    obj.style.top = parseInt(obj.style.top.replace("px","")) + (y - oldY);
    oldX = x;
    oldY = y;
    }
}
//-->
</SCRIPT>
<div id="wnd" onMouseDown="init();" onMouseMove="moveIt(this);" onMouseUp="end();" style="border:1px outset #333333;position:absolute;filter:alpha(opacity=70);top:100;left:200;width:200;height:120;display:inline;">
</div>


------解决方案--------------------
JScript code


var dragapproved=false; 

function drag() 
{ 
   if(!document.all) 
   { 
      return; 
   } 
   if(event.srcElement.className=="myClass") 
   { 
      dragapproved=true; 
      var obj=event.srcElement; 
      var tempX=obj.style.pixelLeft; 
      var tempY=obj.style.pixelTop; 
      var x=event.clientX; 
      var y=event.clientY; 
      document.onmousemove=move; 
      function move() 
      { 
         if(event.button==1 && dragapproved) 
         { 
            obj.style.pixelLeft=tempX+event.clientX-x; 
            obj.style.pixelTop=tempY+event.clientY-y; 
            return false; 
         } 
      } 
   } 
} 
document.onmousedown=drag; 
document.onmouseup=new Function("dragapproved=false");