-
Notifications
You must be signed in to change notification settings - Fork 0
/
遮罩引导02.html
274 lines (212 loc) · 6.92 KB
/
遮罩引导02.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>遮罩引导</title>
</head>
<body>
<div id="need-guide"
style="width: 300px; height: 100px; position: absolute; top: 100px; left: 300px; background: #ddd;">
需要引导的元素
</div>
<div id="need-guide2"
style="width: 100px; height: 50px;">
需要引导的元素2
</div>
<div id="need-guide3"
style="width: 100px; height: 50px;">
需要引导的元素2
</div>
<button onclick="startGuide()">开始引导</button>
<script>
(function(window){
function _createMaskEle () {
var ele = window.document.createElement("div")
ele.setAttribute("id", "maskGuide_mask");
ele.style.position = "fixed";
ele.style.left = 0;
ele.style.top = 0;
ele.style.right = 0;
ele.style.bottiom = 0;
ele.style.height = "100%";
ele.style.width = "100%";
ele.style.background = "#000";
ele.style.opacity = 0.7;
ele.style.zIndex = 999;
return ele;
}
function _createGuideContainerEle(option) {
var ele = window.document.createElement("div")
// ele.setAttribute("id", "maskGuide_guide");
var size = _getGuideSize(option);
var position = _getGuidePosition(option);
ele.style.position = "fixed";
// ele.style.overflow = "hidden";
ele.style.zIndex = 999;
ele.style.background = "#fff";
ele.style.width = size.width + "px";
ele.style.height = size.height + "px";
ele.style.left = position.x + "px";
ele.style.top = position.y + size.height + "px";
if(option.explain){
var explainEle = _createExplainEle(option);
ele.appendChild(explainEle);
}
if(option.showNext || option.showClose){
var actionEle = _createActionEle(option);
ele.appendChild(actionEle);
}
return ele;
};
function _createGuideEle(option) {
var ele = window.document.createElement("div")
ele.setAttribute("id", "maskGuide_guide");
ele.addEventListener("click", option.selfClickCallback);
var size = _getGuideSize(option);
var position = _getGuidePosition(option);
ele.style.position = "fixed";
ele.style.zIndex = 999;
ele.style.opacity = 0.1;
ele.style.background = "#fff";
ele.style.width = size.width + "px";
ele.style.height = size.height + "px";
ele.style.left = position.x + "px";
ele.style.top = position.y + "px";
ele.style.background = "#fff";
ele.style.boxShadow = "0px 0px 10px #fff";
return ele;
};
function _createActionEle(option) {
var ele = window.document.createElement("div")
ele.setAttribute("id", "maskGuide_action");
var nextBtn = window.document.createElement("button");
var clsoeBtn = window.document.createElement("button");
nextBtn.innerHTML = option.nextCallbackText;
clsoeBtn.innerHTML = option.closeCallBackText;
nextBtn.addEventListener("click", option.nextCallback);
clsoeBtn.addEventListener("click", option.closeCallBack);
nextBtn.style.float = "right";
nextBtn.style.marginLeft = "10px";
clsoeBtn.style.float = "right";
ele.appendChild(nextBtn);
ele.appendChild(clsoeBtn);
return ele;
};
function _createExplainEle(option) {
var ele = window.document.createElement("div");
ele.setAttribute("id", "maskGuide_explain");
ele.style.position = "relative";
ele.style.paddingTop = "8px";
var explainEle = window.document.createElement("div");
explainEle.style.background = "#fff";
explainEle.style.position = "relative";
explainEle.style.boxShadow = "0px 0px 10px #fff";
if(Object.prototype.toString.call(option.explain) !== "[object String]"){
explainEle.appendChild(option.explain);
}else{
explainEle.innerHTML = option.explain;
}
var jiantouEle = window.document.createElement("div");
jiantouEle.style.border = "8px solid transparent";
jiantouEle.style.borderBottomColor = "#FFF";
jiantouEle.style.width = 0;
jiantouEle.style.height = 0;
jiantouEle.style.marginTop = "-15px";
jiantouEle.style.marginLeft = "4px";
jiantouEle.style.position = "absolute";
ele.appendChild(jiantouEle);
ele.appendChild(explainEle)
return ele;
};
function _getGuidePosition(option) {
var x = option.width || option.el.getBoundingClientRect().x;
var y = option.width || option.el.getBoundingClientRect().y;
return {
x: x,
y: y
}
}
function _getGuideSize(option) {
var width = option.width || option.el.getBoundingClientRect().width;
var height = option.width || option.el.getBoundingClientRect().height;
return {
width: width,
height: height
}
}
function _clear() {
if(document.getElementById("maskGuide_guide")){
window.document.body.removeChild(document.getElementById("maskGuide_mask"));
}
if(document.getElementById("maskGuide_guide")){
window.document.body.removeChild(document.getElementById("maskGuide_guide"));
}
}
var DEFAULT = {
el: null,
width: null,
height: null,
top: null,
left: null,
selfClickCallback: _clear,
nextCallback: _clear,
closeCallBack: _clear,
nextCallbackText: "下一步",
closeCallBackText: "跳过",
showNext: true,
showClose: false,
explain: null
};
function maskGuide (options, nextCallback, closeCallBack) {
_clear();
var option = Object.assign({}, DEFAULT, options);
if(!(options instanceof Object)){
option.el = options;
option.nextCallback = nextCallback || option.nextCallback;
option.closeCallBack = closeCallBack || option.closeCallBack;
}
// el 可以是id或者dom元素
if(typeof option.el === "string"){
var ele = document.getElementById(option.el);
option.el = ele;
}
if(!option.el){
var errorMsg = "options Element or id not found. not found need guide element";
console.error(errorMsg);
return errorMsg;
}
var maskEle = _createMaskEle();
var guideEle = _createGuideEle(option);
var guideContainerEle = _createGuideContainerEle(option);
// maskEle.appendChild(guideEle);
window.document.body.appendChild(maskEle);
window.document.body.appendChild(guideEle);
window.document.body.appendChild(guideContainerEle);
return maskEle;
};
window.maskGuide = maskGuide;
})(window)
</script>
<script>
function startGuide() {
window.maskGuide("need-guide2", startGuide2)
}
function startGuide2(){
window.maskGuide("need-guide3", startGuide3)
}
function startGuide3(){
var ele = window.document.createElement("div");
var child1 = window.document.createElement("div");
child1.innerHTML = "option.explain";
var child2 = window.document.createElement("div");
child2.innerHTML = "option.explain";
var child3 = window.document.createElement("div");
child3.innerHTML = "option.explain";
ele.appendChild(child1);
ele.appendChild(child2);
ele.appendChild(child3);
window.maskGuide({el:"need-guide",explain:ele})
}
</script>
</body>
</html>