实现页面元素拖放功能的JS

window.onload=function(){
 drag(document.getElementById('drag'),[200,400,30,30]);
 
};

function drag(o,r){
 o.firstChild.onmousedown=function(){return false;};
 o.onmousedown=function(a){
  var d=document;if(!a)a=window.event;
  var x=a.offsetX,y=a.offsetY;
  if(o.setCapture)
   o.setCapture();
  else if(window.captureEvents)
   window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

  d.onmousemove=function(a){
   if(!a)a=window.event;
   if(!a.pageX)a.pageX=a.clientX;
   if(!a.pageY)a.pageY=a.clientY;
   var tx=a.pageX-x,ty=a.pageY-y;
   o.style.left=tx;//<r[0]?r[0]:tx>r[1]?r[1]:tx;
   o.style.top=ty;//<r[2]?r[2]:ty>r[3]?r[3]:ty;
   px=tx;
   py=ty;
  };
  d.onmouseup=function(){
   
   if(o.releaseCapture)
    o.releaseCapture();
   else if(window.captureEvents)
    window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
   d.onmousemove=null;
   d.onmouseup=null;
  };
 };

你可能感兴趣的:(function,null)