drag方法 拖拽
var drag = {
init: function (dom) {
this.dom = dom;
this.bindEvent();
},
bindEvent: function () {
this.dom.onmousedown = this.mouseDown.bind(this);
},
mouseDown: function (e) {
document.onmousemove = this.mouseMove.bind(this);
document.onmouseup = this.mouseUp.bind(this);
this.disX = e.clientX - this.dom.offsetLeft;
this.disY = e.clientY - this.dom.offsetTop;
},
mouseMove: function (e) {
this.newLeft = e.clientX - this.disX;
this.newTop = e.clientY - this.disY;
this.dom.style.left = this.newLeft + 'px';
this.dom.style.top = this.newTop + 'px';
},
mouseUp: function () {
document.onmousemove = null;
document.onmouseup = null;
}
}