-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag.js
39 lines (35 loc) · 883 Bytes
/
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
function drag(id){
var obj = document.getElementById(id);
var disX=0;
var disY=0;
obj.onmousedown=function(ev){
var ev = ev || event;
disX=ev.clientX - obj.offsetLeft;
disY=ev.clientY - obj.offsetTop;
console.log( disX+',' +disY);
document.onmousemove=function(ev){
var ev =ev || event;
var L=ev.clientX - disX;
var T=ev.clientY - disY;
if( L<0){
L=0;
}
else if(L > document.documentElement.offsetWidth - obj.offsetWidth) {
L = document.documentElement.offsetWidth - obj.offsetWidth;
}
if(T<0){
T=0;
}
else if(T>document.documentElement.clientHeight - obj.offsetHeight){
T=document.documentElement.clientHeight - obj.offsetHeight;
}
obj.style.left= L + 'px';
obj.style.top= T + 'px';
}
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
}
return false;
}
}