面向对像的拖拽

function Drag(obj){
   this.oBox= document.getElementById(obj);
   this.disX =0;
   this.disY =0;
   var slef =this;
   this.oBox.onmousedown=function(ev){
      self.dragDown(ev)
  }
}

Drag.prototype={
    dragDown:function(ev){
     
        this.disX =ev.clientX - this.oBox.offsetLeft;
        this.disY = ev.clientY - this.oBox.offsetRight;
        var self = this;
        document.onmousemove = function(ev){
            
           self.dragMove(ev);
        }
         document.onmouseup = function(ev){
            
            document.onmousemove=null;
            document.onmouseup =null;
        }

       return false;
    },
   
   dragMove:function(ev){
    
      var L = ev.clientX -this.disX;
      var Y = ev.clientY - this.disY;
      this.oBox.style.left = L + 'px';
      this.oBox.style.top =Y + 'px';
      
   }
}

你可能感兴趣的:(JavaScript)