-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt.js
379 lines (323 loc) · 10.5 KB
/
prompt.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* --------------------------------------------------
*
* 功能: 扁平化,简单信息提示框。类似七牛后台提示框。
*
* --------------------------------------------------
*
* 依赖: jquery
*
* --------------------------------------------------
*
* 使用:
* 先引入jquery,再引入该js。然后就可以使用。
*
* prompt.add('给我一个理由忘记');
* prompt.add('仿佛有痛楚。如果我晕眩', 'bottom');
*
* --------------------------------------------------
*
* 方法:
* 初始化:init(Object options);
* 添加提示消息:add(String content, String position);
*
* init方法,用来初始化选项的。默认会初始化一次,即默认值。
* add方法,用来添加提示消息。第一个参数是提示消息的内容,第二个参数是提示消息展示的位置。
* 位置仅有三个值可选:'top','bottom','center'。分别表示展示在顶部,底部,中间。该参数可省略。省略或非该三个值,强制为'top'。
*
* --------------------------------------------------
*
* 选项:
* 1. max 信息提示框同时显示的最大个数。默认4
* 2. delay 信息提示框展示的时间,单位毫秒。默认2800
* 3. height 顶部或底部消息提示框时,消息提示框的高度。默认50
* 4. centerWidth 中间模态提示框时,提示框的宽度。默认400
* 5. centerHeight 中间模态提示框时,提示框的高度。默认90
* 6. fontSize 提示框文字的大小。默认16
* 7. color 提示框的文字颜色。 默认#3d995f
* 8. backgroundColor 提示框的背景颜色。默认#d7fae3
* 9. borderColor 提示框的边框颜色。默认#d7fae3
*
* 默认有一套默认值。引入js就可以使用。如果想修改默认值,可以通过init方法。
* 如:prompt.init({'fontSize':14, 'max':3, 'height': 45});
*
* 还可以直接修改。
* 如:prompt.fontSize = 14;
*
* 这些修改,都必须在add方法之前操作。
*
* --------------------------------------------------
*
* author: vini123
*
* web: https://mlxiu.com
*
* blog:https://blog.vini123.com
*
* --------------------------------------------------
*/
var prompt;
(function(factory){
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
}else if (typeof exports === 'object') {
factory(require('jquery'));
}
else{
factory(jQuery);
}
})(function($){
'use strict';
var theself;
var console = window.console || {log:function(value){}};
function Prompt()
{
this.running = 0;
this.list = [];
this.timer = null;
this.init();
theself = this;
$(window).resize(Prompt.resize);
};
Prompt.top = 'top';
Prompt.center = 'center';
Prompt.bottom = 'bottom';
Prompt.analysisOptions = function(options, key, value)
{
if(!options)
return value;
if(!options[key])
return value;
return options[key];
};
Prompt.prototype = {
constructor: Prompt,
init:function(options)
{
this.max = Prompt.analysisOptions(options, 'max', 4);
this.delay = Prompt.analysisOptions(options, 'delay', 2.8 * 1000);
this.height = Prompt.analysisOptions(options, 'height', 50);
this.centerWidth = Prompt.analysisOptions(options, 'centerWidth', 400);
this.centerHeight = Prompt.analysisOptions(options, 'centerHeight', 90);
this.fontSize = Prompt.analysisOptions(options, 'fontSize', '16px');
this.color = Prompt.analysisOptions(options, 'color', '#3d995f');
this.backgroundColor = Prompt.analysisOptions(options, 'backgroundColor', '#d7fae3');
this.borderColor = Prompt.analysisOptions(options, 'borderColor', '#d7fae3');
},
add:function(content, position = null)
{
if(!content)return;
if(!position || (position != Prompt.top && position != Prompt.center && position != Prompt.bottom)){
position = Prompt.top;
}
theself.list.push({content:content, position:position});
Prompt.run();
}
};
Prompt.run = function()
{
if(!theself.list || theself.list.length <= 0 || theself.running >= theself.max)
{
return;
}
var curMsg = theself.list.shift();
if(curMsg.position == Prompt.center)
{
theself.list.unshift(curMsg);
Prompt.stopTimer();
Prompt.nextCenterListener();
return;
}
if(theself.runCenter)
{
theself.list.unshift(curMsg);
return;
}
var samePositionNum = 0;
$('.prompt').each(function(index){
if($(this).data('position') == curMsg.position)
{
samePositionNum ++;
}
});
var distance = (samePositionNum + 0.5) * theself.height;
var html,element;
if(curMsg.position == Prompt.top)
{
html = Prompt.sprintf('<div class="prompt" data-position="%s" style="position:fixed;display:inline-block;width:100%;height:%s;line-height:%s;top:%s;left:0;background-color:%s;color:%s;border:1px solid %s;font-size:%s;font-weight:700;z-index:999;text-align:center;opacity:0;">%s</div>', curMsg.position, theself.height + 'px', theself.height + 'px', distance + 'px',
theself.backgroundColor, theself.color, theself.borderColor, theself.fontSize, curMsg.content);
element = $(html);
element.animate({'top': (distance - theself.height * 0.5), 'opacity':1}, 'swing');
}
else if(curMsg.position == Prompt.bottom)
{
$('.prompt').each(function(){
if($(this).data('position') == Prompt.bottom)
{
var bottom = $(this).data('bottom') + theself.height;
$(this).data('bottom', bottom);
$(this).stop(false, true);
$(this).animate({'bottom':bottom}, 'swing');
}
})
html = Prompt.sprintf('<div class="prompt" data-bottom="%s" data-position="%s" style="position:fixed;display:inline-block;width:100%;height:%s;line-height:%s;bottom:%s;left:0;background-color:%s;color:%s;border:1px solid %s;font-size:%s;font-weight:700;z-index:999;text-align:center;opacity:0;">%s</div>', 0, curMsg.position, theself.height + 'px', theself.height + 'px', -theself.height + 'px',
theself.backgroundColor, theself.color, theself.borderColor, theself.fontSize, curMsg.content);
element = $(html);
element.animate({'bottom': 0, 'opacity':1}, 'swing');
}
$('body').append(element);
theself.running ++;
Prompt.startTimer();
};
Prompt.complete = function()
{
var firstElement;
var firstPosition;
var samePositionNum = 0;
var otherPositionNum = 0;
$('.prompt').each(function(){
if(!firstElement)
{
firstElement = $(this);
firstPosition = firstElement.data('position');
if(firstPosition == Prompt.top)
{
firstElement.animate({'top': (parseInt($(this).css('top')) - theself.height), 'opacity': 0}, 600, null, function(){
firstElement.remove();
firstElement = null;
theself.running --;
Prompt.run();
});
}
else if(firstPosition == Prompt.bottom)
{
firstElement.animate({'bottom': (parseInt($(this).css('bottom')) + theself.height), 'opacity': 0}, 600, null, function(){
firstElement.remove();
firstElement = null;
theself.running --;
Prompt.run();
});
}
return true;
}
if(firstPosition == $(this).data('position'))
{
samePositionNum ++;
}
else
{
otherPositionNum ++;
}
})
var otherElement;
$('.prompt').each(function(i){
if(i == 0)
return true;
if(samePositionNum > 0)
{
if($(this).data('position') == firstPosition)
{
if($(this).data('position') == Prompt.top)
{
$(this).animate({'top': (parseInt($(this).css('top')) - theself.height)}, 600);
}
}
}
if(otherPositionNum > 0)
{
if($(this).data('position') != firstPosition)
{
if(!otherElement)
{
otherElement = $(this);
if(otherElement.data('position') == Prompt.top)
{
otherElement.animate({'top': (parseInt($(this).css('top')) - theself.height), 'opacity': 0}, 600, null, function(){
otherElement.remove();
otherElement = null;
theself.running --;
Prompt.run();
});
}
else if(otherElement.data('position') == Prompt.bottom)
{
otherElement.animate({'bottom': (parseInt($(this).css('bottom')) + theself.height), 'opacity': 0}, 600, null, function(){
otherElement.remove();
otherElement = null;
theself.running --;
Prompt.run();
});
}
return true;
}
if($(this).data('position') == Prompt.top)
{
$(this).animate({'top': (parseInt($(this).css('top')) - theself.height)}, 600);
}
}
}
})
if($('.prompt').length <= 0)
{
Prompt.stopTimer();
}
};
Prompt.resize = function(){
$('.prompt').each(function(){
$(this).css('width', $(window).width());
if($(this).data('position') == Prompt.center)
{
$(this).css('height', $(window).height());
}
});
};
// 当下一个提示框是居中提示,阻塞后续展示。当之前的提示框展示完毕,才开始从居中这个开始展示。
Prompt.nextCenterListener = function(){
if($('.prompt').length > 0)
return;
var curMsg = theself.list.shift();
var html = Prompt.sprintf('<div class="prompt" data-position="%s" style="position:fixed;width:100%;height:100%;background-color:rgba(0,0,0, 0.72);top:0;left:0;"><div style="position:fixed;display:inline-block;width:%s;height:%s;line-height:%s;text-align:center;font-size:%s;font-weight:700;left:50%;top:50%; margin-left:%s; margin-top:%s; border-radius:6px;background-color:%s;color:%s;border:1px solid %s;">%s</div></div>', curMsg.position, theself.centerWidth + 'px', theself.centerHeight + 'px', theself.centerHeight + 'px', theself.fontSize, -theself.centerWidth * 0.5 + 'px', -theself.centerHeight * 0.5 + 'px', theself.backgroundColor, theself.color, theself.borderColor,curMsg.content);
$('body').append($(html));
$('.prompt').bind('click', Prompt.centerElementClick);
theself.runCenter = true;
};
Prompt.centerElementClick = function(){
$(this).animate({opacity:0}, 500, null, function(){
$('.prompt').unbind('click');
$(this).remove();
theself.runCenter = false;
for(var i = 0; i < theself.max; i++)
{
Prompt.run();
}
})
};
Prompt.startTimer = function()
{
if(!theself.timer)
{
theself.timer = setInterval(Prompt.complete, theself.delay);
}
};
Prompt.stopTimer = function()
{
if(!theself.timer)
{
clearInterval(theself.timer);
theself.timer = null;
}
};
Prompt.sprintf = function(){
var arg = arguments;
if(arg.length <= 0){
console.log('you must fill params!');
return '';
}
var str = arg[0];
for(var i = 1; i< arg.length; i++){
str = str.replace(/%s/, arg[i]);
}
return str;
};
prompt = new Prompt();
})