精简版拖动

/* 鼠标拖动 */
var oDrag = "";
var ox,oy,nx,ny,dy,dx;
function drag(e,o){
 var e = e ? e : event;
 var m = document.all ? 1 : 0;
 if(e.button == m)
 {
  if (o.parentNode)
  {
   oDrag = o.parentNode;
  }
  else{oDrag = o;}
  ox = e.clientX;
  oy = e.clientY;  
 }
}
function dragPro(e){
 if(oDrag != "")
 { 
  //var obj=document.getElementById("msg");//拖动的id
  var obj=oDrag;//拖动的id
  var e = e ? e : event;
  obj.style.position = 'absolute';
  dx = parseInt(obj.style.left);
  dy = parseInt(obj.style.top);
  if(isNaN(dx)){dx=0;}
  if(isNaN(dy)){dy=0;}
  nx = e.clientX;
  ny = e.clientY;
  obj.style.left = (dx + ( nx - ox )) + "px";
  obj.style.top = (dy + ( ny - oy )) + "px";
  ox = nx;
  oy = ny;
 }
}
document.onmouseup = function(){oDrag = "";}
document.onmousemove = function(event){dragPro(event);}


调用的时候 onmousedown='drag(event,this)'

不会用博客园的贴效果……演示无法提供……

你可能感兴趣的:(精简版拖动)