-
Notifications
You must be signed in to change notification settings - Fork 6
/
task6.html
304 lines (266 loc) · 11.8 KB
/
task6.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>任务六:UI组件之浮出层</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
body > div {
position: relative;
width: 100%;
height: 2000px;
background-color: #A6596D;
}
#mask {
position: absolute;
top: 0;
left: 0;
display: none;
width: 100%;
height: 100%;
background-color: #000;
opacity: .5;
}
#float-layer {
position: fixed;
display: none;
width: 30%;
height: 300px;
background-color: #69C998;
z-index: 10;
}
#fl-header {
height: 40px;
line-height: 40px;
background-color: #ccc;
text-indent: 1em;
cursor: move;
}
#float-layer > p {
text-indent: 1em;
}
#float-layer > input {
position: absolute;
bottom: 0;
right: 0;
margin: 4px;
}
#float-layer > input:first-of-type {
right: 50px;
}
/*调整面板大小*/
.ui-resizable-r {
position: absolute;
right: 0;
top: 0;
width: 10px;
height: 100%;
cursor: e-resize;
}
.ui-resizable-b {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 10px;
cursor: s-resize;
}
.ui-resizable-rb {
position: absolute;
right: 0;
bottom: 0;
width: 10px;
height: 10px;
cursor: nw-resize;
}
</style>
</head>
<body>
<div>
<input id="login-btn" type="button" value="登录" />
<div id="mask" onselectstart="return false;">
<div id="float-layer" onselectstart="return false;">
<div id="fl-header" onselectstart="return false;"> 这是一个浮出层</div><!-- 禁止鼠标选中事件,防止元素到界面边界仍可以单独拖出的情况 -->
<p> 这是一个浮出层</p>
<input id="sure-btn" type="button" value="确定" />
<input id="cancel-btn" type="button" value="取消" />
</div>
</div>
</div>
<script type="text/javascript" src="EventUtil.js"></script>
<script type="text/javascript">
var $ = function(ele) {
return document.querySelector(ele);
};
var FloatLayer = function (ele) {
this.element = ele;
this.mask = ele.parentNode;
this.isDragging = false;
this.mouseOffsetX = 0; //鼠标按下时相对于浮层左上角位置
this.mouseOffsetY = 0;
this.init();
};
FloatLayer.prototype = {
show: function () {
this.mask.style.display = 'block';
this.element.style.display = 'block';
//可视窗口大小
var wWidth = document.documentElement.clientWidth || document.body.clientWidth;
var wHeight = document.documentElement.clientHeight || document.body.clientHeight;
//浮出层元素大小
var eWidth = this.element.offsetWidth; //display:none不能获取offsetWidth
var eHeight = this.element.offsetHeight;
this.element.style.left = (wWidth - eWidth) / 2 + 'px';
this.element.style.top = (wHeight - eHeight) / 2 + 'px';
},
hide: function () {
this.mask.style.display = 'none';
this.element.style.display = 'none';
},
init: function () {
var self = this;
EventUtil.addHandler(this.mask, 'click', function () {
self.hide();
});
//阻止事件冒泡
EventUtil.addHandler(this.element, 'click', function (e) {
e = EventUtil.getEvent(e);
EventUtil.stopPropagation(event);
});
EventUtil.addHandler($('#fl-header'), 'mousedown', function (e) {
e = EventUtil.getEvent(e);
self.isDragging = true;
self.mouseOffsetX = e.pageX - self.element.offsetLeft;//注意元素移动offsetLeft也会移动
self.mouseOffsetY = e.pageY - self.element.offsetTop;
});
EventUtil.addHandler(document, 'mousemove', function (e) {//鼠标移动可能会超出#fl-header的范围,故在document上添加移动事件
if (self.isDragging === true) {
e = EventUtil.getEvent(e);
var moveX = e.pageX - self.mouseOffsetX;//浮层新位置
var moveY = e.pageY - self.mouseOffsetY;
var wWidth = document.documentElement.clientWidth || document.body.clientWidth;
var wHeight = document.documentElement.clientHeight || document.body.clientHeight;
var eWidth = self.element.offsetWidth;
var eHeight = self.element.offsetHeight;
var maxX = wWidth - eWidth;
var maxY = wHeight - eHeight;
moveX = moveX > 0 ? (moveX > maxX ? maxX : moveX) : 0;
moveY = moveY > 0 ? (moveY > maxY ? maxY : moveY) : 0;
self.element.style.left = moveX + 'px';
self.element.style.top = moveY + 'px';
}
});
EventUtil.addHandler(document, 'mouseup', function (e) {//在document上添加鼠标弹起事件
self.isDragging = false;
});
}
};
var createFloatLayer = function (ele) {
return new FloatLayer(ele);
};
/**
*右、下、右下调整面板大小
*@param {string} panelId 面板元素id
*@param {number} minWidth 可调整到的最小宽度
*@param {number} minHeight 可调整到的最小高度
*/
var resizePanel = function (panelId, minWidth, minHeight) {
var panel = document.getElementById(panelId);
minWidth = minWidth - 10;
minHeight = minHeight -10;
var Control = function (type) {
this.type = type;//控制元素类型 'r','b','rb'
this.isMoving = false;
this.startX = 0;
this.startY = 0;
this.init();
};
Control.prototype = {
init: function () {
this.element = document.createElement("div");
this.element.className = "ui-resizable-" + this.type;
this.element.classList.add("ui-resizable-ctrl");
panel.appendChild(this.element);
var self = this;
this.element.addEventListener("mousedown", function (e) {
self.isMoving = true;
var e = e || window.event;
self.startX = e.pageX - self.element.offsetLeft;//鼠标按下时鼠标相对于控制元素的位置
self.startY = e.pageY - self.element.offsetTop;
//拖动过程中触发了浏览器的 drag 操作,导致mouseup丢失的解决方法
(function (event){
//不仅仅要stopPropagation,还要preventDefault
if(event.stopPropagation)
event.stopPropagation();
if(event.preventDefault)
event.preventDefault();
event.cancelBubble=true;
event.returnValue=false;
return false;
}(e));
}, false);
document.addEventListener("mousemove", function (e) {
if (self.isMoving) {
var e = e || window.event;
var ctrlMoveX = e.pageX - self.startX;//控制元素在父元素中的新位置
var ctrlMoveY = e.pageY - self.startY;
//最大不超过页面
var bodyWidth = document.documentElement.clientWidth || document.body.clientWidth;
var bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
var panelX = panel.offsetLeft;
var panelY = panel.offsetTop;
var ctrlMaxX = bodyWidth - panelX - self.element.offsetWidth;
var ctrlMaxY = bodyHeight - panelY - self.element.offsetHeight;
ctrlMoveX = ctrlMoveX > minWidth ? (ctrlMoveX > ctrlMaxX ? ctrlMaxX : ctrlMoveX) : minWidth;
ctrlMoveY = ctrlMoveY > minHeight ? (ctrlMoveY > ctrlMaxY ? ctrlMaxY : ctrlMoveY) : minHeight;
switch(self.type) {
case 'r':
self.element.style.left = ctrlMoveX + 'px';
panel.style.width = ctrlMoveX + self.element.offsetWidth + 'px';//加上控制元素的宽度
break;
case 'b':
self.element.style.top = ctrlMoveY + 'px';
panel.style.height = ctrlMoveY + self.element.offsetHeight + 'px';
break;
case 'rb':
self.element.style.left = ctrlMoveX + 'px';
self.element.style.top = ctrlMoveY + 'px';
panel.style.width = ctrlMoveX + self.element.offsetWidth + 'px';
panel.style.height = ctrlMoveY + self.element.offsetHeight + 'px';
break;
}
}
}, false);
document.addEventListener("mouseup", function () {
self.isMoving = false;
var ctrls = document.getElementsByClassName("ui-resizable-ctrl");
for (var i = 0, len = ctrls.length; i < len; i++) {
ctrls[i].style.left = "";//style属性只能获取内联样式的值,设为空表示恢复默认,即样式表中的设置,这样鼠标弹起就会恢复右、下、右下的位置。
ctrls[i].style.top = "";
}
}, false);
}
};
var rCtrl = new Control("r");
var bCtrl = new Control("b");
var rbCtrl = new Control("rb");
};
(function () {
var layer = createFloatLayer($('#float-layer'));
EventUtil.addHandler($('#login-btn'), 'click', function () {
layer.show();
});
EventUtil.addHandler($('#sure-btn'), 'click', function () {
layer.hide();
});
EventUtil.addHandler($('#cancel-btn'), 'click', function () {
layer.hide();
});
resizePanel("float-layer", 100, 90);
})();
</script>
</body>
</html>