-
Notifications
You must be signed in to change notification settings - Fork 0
/
继承 引用 原型.html
121 lines (110 loc) · 2.23 KB
/
继承 引用 原型.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!Doctype HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>面向对象 拖拽</title>
<style>
#div{
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 100px;
left: 100px;
}
#div1{
width: 100px;
height: 100px;
background: yellow;
position: absolute;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div id="div"></div>
<div id="div1">LimitDrag</div>
</body>
<script>
window.onload=function()
{
new Drag('div');
new LimitDrag('div1');
}
function Drag(id)
{
var _this=this;
/*解决遇到事件后this指向改变的问题*/
this.disX=0;
this.disY=0;
this.oDiv= document.getElementById(id);
this.oDiv.onmousedown=function()
{
_this.fnDown();/*遇到事件this指向改变 的解决方法*/
return false;/*处理 拖拽小BUG*/
}
}
Drag.prototype.fnDown=function(ev)
{
var _this=this;
var oEven=ev||event;
this.disX=oEven.clientX-this.oDiv.offsetLeft;
this.disY=oEven.clientY-this.oDiv.offsetTop;
document.onmousemove=function()
{
_this.fnMove();
/*遇到事件this指向改变 的解决方法*/
};
document.onmouseup=function()
{
_this.fnUp();/*遇到事件this指向改变 的解决方法*/
};
};
Drag.prototype.fnMove=function(ev)
{
var oEven=ev||event;
this.oDiv.style.left=oEven.clientX-this.disX+'px';
this.oDiv.style.top =oEven.clientY-this.disY+'px';
};
Drag.prototype.fnUp=function()
{
document.onmousemove=null;
document.onmouseup=null;
};
/*******************引用 继承*****************/
function LimitDrag(id)
{
/*继承 父级属性 */
Drag.call(this, id);
}
for(var i in Drag.prototype)/*原型链*/
{
LimitDrag.prototype[i]=Drag.prototype[i];
}
LimitDrag.prototype.fnMove=function(ev)
{
var oEven=ev||event;
var oLeft=oEven.clientX-this.disX;
var oTop =oEven.clientY-this.disY;
if(oLeft<0)
{
oLeft=0;
}
else if(oLeft>document.documentElement.clientWidth-this.oDiv.offsetWidth)
{
oLeft=document.documentElement.clientWidth-this.oDiv.offsetWidth;
}
if(oTop<0)
{
oTop=0;
}
else if(oTop>document.documentElement.clientHeight-this.oDiv.offsetHeigh)
{
oTop=document.documentElement.clientHeigh-this.oDiv.offsetHeigh;
}
this.oDiv.style.left=oLeft+'px';
this.oDiv.style.top=oTop+'px';
}
</script>
</html>