面向对象拖拽

function Drag(){

this.logo=document.getElementsByClassName("f-logo")[0];

this.enter=document.getElementsByName("enter")[0];

this.logo.onmousedown = function(e){
  _this.down(e);
  }

}

Drag.prototype.down = function(e){
  var _this = this;
  var e = e||event;
  this.divX = e.offsetX;
  this.divY = e.offsetY;
  document.onmousemove = function(e){
    _this.mov(e)
  }
  document.onmouseup = function(){
    _this.up()
  }
}
Drag.prototype.mov = function(e){
  var e = e||event;
  this.enter.style.left = e.clientX- this.divX +'px';
  this.enter.style.top = e.clientY-this.divY+'px';
}
Drag.prototype.up = function(){
  document.onmousemove = null;
  document.onmouseup = null;
}

new Drag();

posted @ 2018-03-01 13:20  一粒丶红尘  阅读(101)  评论(0编辑  收藏  举报