-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path拖拽
68 lines (62 loc) · 2.08 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
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
<style>
#div2 {
width: 400px;
height: 300px;
background:red;
position: relative;
}
#div1 {
width: 100px;
height: 100px;
background: #ccc;
position: absolute;
}
</style>
<script>
window.onload = function ()
{
var oDiv = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
var disX=0;
var disY=0;
oDiv.onmousedown = function (ev) {
var oEvent = ev || event;
disX = oEvent.clientX - oDiv.offsetLeft;
disY = oEvent.clientY - oDiv.offsetTop;
document.onmousemove = function (ev) {
var oEvent = ev || event;
var l = oEvent.clientX - disX;
var t = oEvent.clientY - disY;
if (l < 0) {
l = 0;
} else if (l > /*document.documentElement.clientWidth*/oDiv2.offsetWidth - oDiv.offsetWidth) {
l = /*document.documentElement.clientWidth*/oDiv2.offsetWidth - oDiv.offsetWidth;
}
if (t < 0) {
t = 0;
} else if (t > /*document.documentElement.clientHeight*/oDiv2.offsetHeight - oDiv.offsetHeight) {
t =/* document.documentElement.clientHeight*/oDiv2.offsetHeight - oDiv.offsetHeight;
}
oDiv.style.left = l + 'px';
oDiv.style.top = t + 'px';
}
document.onmouseup=function(){
document.onmousemove=null;
}
return false;
}
}
</script>
</head>
<body>
<div id="div2">
<div id="div1"></div>
</div>
</body>
</html>