-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv拖拽1.html
59 lines (56 loc) · 1.39 KB
/
div拖拽1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
*{
margin:0;
padding:0;
}
body{
height:2000px;
}
#div{
position:absolute;
width:150px;
height:200px;
background-color: red;
}
</style>
<body>
<div id="div"></div>
<script>
window.onload = function(){
console.log(document);
var oDiv = document.getElementById("div");
oDiv.onmousedown = function(e){
e = e || event;
var iDisX = e.clientX - oDiv.offsetLeft;
var iDisY = e.clientY - oDiv.offsetTop;
document.onmousemove = function(e){
e = e || event;
oDiv.style.left = e.clientX - iDisX + "px";
oDiv.style.top = e.clientY - iDisY + "px";
if(oDiv.offsetLeft >= document.body.offsetWidth - oDiv.offsetWidth){
oDiv.style.left = document.body.offsetWidth - oDiv.offsetWidth + "px";
}
if(oDiv.offsetLeft <= 0){
oDiv.style.left = 0;
}
if(oDiv.offsetTop >= document.body.offsetHeight - oDiv.offsetHeight){
oDiv.style.top = document.body.offsetHeight - oDiv.offsetHeight + "px";
}
if(oDiv.offsetTop <=0){
oDiv.style.top = 0;
}
}
}
document.onmouseup = function(){
document.onmousemove = null;
}
}
</script>
</body>
</html>