forked from pod4g/tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
45 lines (44 loc) · 1.28 KB
/
drag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
*
* author:pod
* QQ:455322185
* date:2016.03.10
* 使用方法:把需要进行拖拽的元素传入构造函数即可
* 例如
* var dragedDOM = document.getElementById('drag');
* var drag = new Drag(gragedDOM);
*
*/
;(function(w,doc,undefined){
var Drag = function(obj){
this.obj = obj;
this.main();
}
Drag.prototype = {
constructor:Drag,
addStyle:function(){
this.obj.style.cssText = "position:absolute;left:"+this.obj.offsetLeft + "px;top:" + this.obj.offsetTop + "px;";
},
bindEvent:function(){
var self = this.obj;
self.onmousedown = function(e){
e = e || event;
var posX = e.clientX - this.offsetLeft;
var posY = e.clientY - this.offsetTop;
doc.onmousemove = function(e){
e = e || event;
self.style.left = e.clientX - posX + "px";
self.style.top = e.clientY - posY + "px";
}
}
self.onmouseup = function(){
doc.onmousemove = null;
};
},
main:function(){
this.addStyle();
this.bindEvent();
}
}
w.Drag = Drag;
})(window,document);