-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3-jsDragMove-1.html
More file actions
58 lines (50 loc) · 1.3 KB
/
3-jsDragMove-1.html
File metadata and controls
58 lines (50 loc) · 1.3 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js drag demo</title>
</head>
<body>
<style>
#box{
width:200px;
height:200px;
background:red;
position:absolute;
}
</style>
<div id="box"></div>
<script>
window.onload = function(){
var box = document.getElementById("box");
box.onmousedown = function(e){
var e = e || event;
var target = e.target || e.srcELement;
var disX = e.clientX - target.offsetLeft;
var disY = e.clientY - target.offsetTop;
document.onmousemove = function(e){
var e = e || event;
target.style.left = e.clientX - disX + "px";
target.style.top = e.clientY - disY + "px";
if(parseInt(target.style.left) <= 0){
target.style.left = 0 + "px";
};
if((parseInt(target.style.left) + target.offsetWidth) >= document.documentElement.clientWidth){
target.style.left = document.documentElement.clientWidth - target.offsetWidth + "px";
};
if(parseInt(target.style.top) <= 0){
target.style.top = 0 + "px";
};
if((parseInt(target.style.top) + target.offsetHeight) >= document.documentElement.clientHeight){
target.style.top = document.documentElement.clientHeight - target.offsetHeight + "px";
};
};
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
}
}
</script>
</body>
</html>