forked from loveyuxiang/JavaScript----demo2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path鼠标拖动1.html
49 lines (44 loc) · 1.49 KB
/
鼠标拖动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
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Event</title>
<style type="text/css">
body{margin:0px;padding:0px;text-align:center;background:#000;}
</style>
</head>
<body>
<div id="box" style="width:200px;height:200px;background:#f00000;position:absolute;top:0;left:0">
</div>
</body>
<script type="text/javascript" charset="utf-8">
var box = document.getElementById("box");
var srcX;
var srcY;
var desX;
var desY;
var w;
var h;
var isDown = false;
box.onmousedown = function(event){
var ent = event||window.event;
srcX = ent.clientX;
srcY = ent.clientY;
w = srcX - parseInt(this.style.left);
h = srcY - parseInt(this.style.top);
isDown = true;
}
box.onmousemove = function(event){
if(isDown){
var ent = event||window.event;
desX = ent.clientX;
desY = ent.clientY;
//this.innerHTML = w;
this.style.top = desY - h + "px";
this.style.left = desX - w + "px";
}
}
window.document.onmouseup = function(){
isDown = false;
}
</script>
</html>