-
Notifications
You must be signed in to change notification settings - Fork 0
/
move10.html
80 lines (74 loc) · 1.95 KB
/
move10.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html>
<head>
<title>运动10</title>
<meta charset="utf-8">
<meta name="keyword" content="继承,拖拽,运动">
<meta name="descript" content="继承拖拽运动实例">
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
</style>
<script type="text/javascript">
function Drag(id){
var _this = this;
var disX = 0;
var disY = 0;
this.oDiv = document.getElementById(id);
this.oDiv.onmousedown = function(ev){
_this.Down(ev);
return false;
}
}
Drag.prototype.Down = function(ev){
var _this = this;
var Event = ev || event;
this.disX = Event.clientX - this.oDiv.offsetLeft;
this.disY = Event.clientY - this.oDiv.offsetTop;
document.onmousemove = function(ev){
_this.Move(ev);
}
document.onmouseup = function(ev){
_this.Up(ev);
}
}
Drag.prototype.Move = function(ev){
var Event = ev || event;
this.oDiv.style.left = Event.clientX - this.disX + 'px';
this.oDiv.style.top = Event.clientY - this.disY + 'px';
}
Drag.prototype.Up = function(ev){
document.onmousedown = null;
document.onmousemove = null;
}
function LimitDrag(id){
Drag.call(this,id);
}
for(var i in Drag.prototype){
LimitDrag.prototype[i] = Drag.prototype[i];
}
LimitDrag.prototype.Move = function(ev){
var Event = ev || event;
var l = Event.clientX - this.disX;
var t = Event.clientY - this.disY
if(l<0)
l = 0;
if(t<0)
t = 0;
this.oDiv.style.left = l + 'px';
this.oDiv.style.top = t + 'px';
}
window.onload = function(){
new Drag('Div1');
new LimitDrag('Div2');
}
</script>
</head>
<body>
演示了继承‘拖拽运动’的实例
<div id="Div1" style="width: 200px; height: 200px; background: yellow; position: absolute;">这是一个可以出界的盒子</div>
<div id="Div2" style="width: 200px; height: 200px; background: green; position: absolute;">这是一个被限制出左界和上界的盒子</div>
</body>
</html>