-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag.html
51 lines (50 loc) · 1.28 KB
/
drag.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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>一个JS拖动</title>
<style>
#log{
width: 100px;
height: 100px;
border: 1px solid #612e76;
position: absolute;
left: 200px;
}
.drag{
width: 100px;
height: 100px;
border: 1px solid #612e76;
position: absolute;
left: 600px;
}
</style>
</head>
<body>
<div id="demo" style="width:100px; height:100px; position:absolute; background-color:silver;"></div>
</body>
</html>
<script>
function drag(){
var demo=document.getElementById('demo');
console.log(demo);
var x,y=0;
demo.onmousedown=function(e){
e = e || event;
x= e.clientX-demo.offsetLeft;
y= e.clientY-demo.offsetTop;
console.log("x"+x);
demo.onmousemove=function(e){
e = e || event;
demo.style.left= e.clientX-x +"px";
demo.style.top= e.clientY-y +"px";
console.log("left"+demo.left);
};
demo.onmouseup=function(){
demo.onmousemove=null;
demo.onmouseup=null;
}
};
}
drag();
</script>