-
Notifications
You must be signed in to change notification settings - Fork 6
/
031-拖拽.html
61 lines (60 loc) · 1.41 KB
/
031-拖拽.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
#div1{ width:200px; height:100px;position:absolute; border:1px solid #232222; }
#div2{ width:100%; height:20px; background:#ccc;}
</style>
<script>
window.onload = function()
{
var oDiv1 = document.getElementById("div1");
var oDiv2 = document.getElementById("div2");
var disX = 0;
var disY = 0;
oDiv2.onmousedown = function (ev)
{
var oEvent = ev || event;
disX = oEvent.clientX - oDiv1.offsetLeft;
disY = oEvent.clientY - oDiv1.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 || document.body.clientWidth) - oDiv1.offsetWidth){
l = (document.documentElement.clientWidth || document.body.clientWidth) - oDiv1.offsetWidth;
}
if(t < 0)
{
t = 0;
}
else if(t > (document.documentElement.clientHeight || document.body.clientHeight) - oDiv1.offsetHeight)
{
t = (document.documentElement.clientHeight || document.body.clientHeight) - oDiv1.offsetHeight
}
oDiv1.style.left = l + "px";
oDiv1.style.top = t + "px";
}
document.onmouseup = function(ev)
{
document.onmousemove = null;
document.onmouseup = null;
}
return false;
}
}
</script>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>