当前位置: 首页 » 前端开发 » js+css3之移动端消息框
 |  全屏浏览  |  字号调整:

js+css3之移动端消息框

2015-04-30 11:34:54 前端开发 0 935 百度未收录
本文最后更新于2015-04-30,已超过 1年没有更新,如果文章内容、图片或者下载资源失效,请留言反馈,我会及时处理,谢谢!
历史上的今天:10月20日 - 1962年,中印边境自卫反击战爆发

周六晚上好, 本文除去代码部分,共10394个字,预计阅读时间需要26分钟。

web移动开发中,经常会用到几种常用的消息提示框,比如正在加载,操作成功,操作失败等。

请点击下面result预览demo。

源码如下:

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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js+css3之移动端消息框</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1.0" />
<style>
body,div,p,span{margin:0;padding:0;}
body{font-family: "Microsoft Yahei","微软雅黑",Arial,Tahoma;font-size:14px;}
input{border:0 none;width:70px;height:30px;line-height:30px;background-color:#eee;color:#666;border-radius:4px;}
input:focus{outline:0 none;}
 
.btn-container{width:320px;height:100px;margin:100px auto;text-align:center;}
 
/*插件css*/
#animationTipBox{
	width:180px;
	height:auto;
	background-color:#fff;
	border-radius:8px;
	position:fixed;
	left:50%;
	top:50%;
	margin:-75px 0 0 -90px;
	z-index:1001;
	-webkit-animation:alertAnimation 0.3s ease-in-out 0 1; 
	animation:alertAnimation 0.3s ease-in-out 0 1; 
}
#sm-load{
	width:20px;
	height:20px;
	background:none;
	padding:0;	
	position:relative;
	float:left;
	vertical-align:middle;
	margin-right:5px;
	top:0;
	left:0;
	-webkit-animation:none;	
	animation:none;	
}
#sm-load .load{
	width:20px;
	height:20px;
	border:0 none;
	margin:0 auto;
	top:0;
}
#sm-load .icon_box{
	margin:0 auto;
}
#sm-load .load .cirBox1,#sm-load  .load .cirBox2,#sm-load  .load .cirBox3{
	width: 20px;
	height: 20px;
	position: absolute;
	left: 0;
	top: 0;
}
#sm-load .load .cirBox1 > div,#sm-load  .load .cirBox2 > div,#sm-load  .load .cirBox3 > div{
	width: 4px;
	height:4px;
	border-radius: 100%;
	background-color: #fff;
	position: absolute;
}
#sm-load .load .icon_box{
	width:20px;
	height:20px;
}
#animationTipBox .icon{
	position:relative;
	width:80px;
	height:80px;
	border-radius:50px;
	border:4px solid #66cc33;
	margin:15px auto 5px;
}
#animationTipBox .icon_box{
	width:80px;
	height:80px;
	margin:0 auto;
	text-align:center;
	position:relative;
}
.tip .icon{
	width:80px;
	height:80px;
	line-height:80px;	
	background-color:#66cc33;
	border-radius:100%;
	color:#fff;
	font-size:80px;
	text-align:center;
}
#animationTipBox .lose .icon{
	border-color:#FF9090;
}
#animationTipBox .lose .icon_box{
	-webkit-animation:lose_Animation 0.5s;
	animation:lose_Animation 0.5s;
}
#animationTipBox .dec_txt{
	font-size:16px;  
	text-align:center; 
	color:#666;
	line-height:26px;
	height:26px;
	padding:5px 0 10px;
}
.success .line_short{
	width:25px;
	height:5px;
	position:absolute;
	left: 14px;
	top: 46px;
	-webkit-border-radius:4px;
	border-radius:4px;
	background-color:#66cc33;
	-webkit-transform:rotate(45deg);
	transform:rotate(45deg);
	-webkit-animation:success_short_Animation 0.65s;
	animation:success_short_Animation 0.65s;
}
.success .line_long{
	width:47px;
	height:5px;
	position:absolute;
	right: 8px;
	top: 38px;
	-webkit-border-radius:4px;
	border-radius:4px;	
	background-color:#66cc33;
	-webkit-transform:rotate(-45deg);
	transform:rotate(-45deg);
	-webkit-animation:success_long_Animation 0.65s;
	animation:success_long_Animation 0.65s;
}
 
.lose .line_left,.lose .line_right{
	width:47px;
	height:5px;
	position:absolute;
	left: 17px;
	top: 37px;
	-webkit-border-radius:4px;
	border-radius:4px;
	background-color:#FF9090;
	-webkit-transform:rotate(45deg);
	transform:rotate(45deg);	
}
.lose .line_right{	
	right: 11px;
	top: 37px;
	-webkit-transform:rotate(-45deg);
	transform:rotate(-45deg);	
}
 
 
/* tipBoxKeyFrames */
	@keyframes alertAnimation{
		0% {
			transform: scale(0.5);
		}
		45% {
			transform: scale(1.25);
		}
		80% {
			transform: scale(0.95);
		}
		100% {
			transform: scale(1);
		} 
	}
	@-webkit-keyframes alertAnimation{
		0% {
			-webkit-transform: scale(0.5); }
		45% {
			-webkit-transform: scale(1.25); }
		80% {
			-webkit-transform: scale(0.95); }
		100% {
			-webkit-transform: scale(1);
		} 
	}
/* tipBoxKeyFrames */
 
 
/*success_Animation*/ 
	@-webkit-keyframes success_short_Animation{
		0% {
			width: 0;
			left: 1px;
			top: 19px;
		}
		54% {
			width: 0;
			left: 1px;
			top: 19px;
		}
		70% {
			width: 50px;
			left: -4px;
			top: 37px;
		}
		84% {
			width: 17px;
			left: 21px;
			top: 48px;
		}
		100% {
			width: 25px;
			left: 14px;
			top: 45px;
		}
	}
	@keyframes success_short_Animation{
		0% {
			width: 0;
			left: 1px;
			top: 19px;
		}
		54% {
			width: 0;
			left: 1px;
			top: 19px;
		}
		70% {
			width: 50px;
			left: -4px;
			top: 37px;
		}
		84% {
			width: 17px;
			left: 21px;
			top: 48px;
		}
		100% {
			width: 25px;
			left: 14px;
			top: 45px;			
		}
	}
	@-webkit-keyframes success_long_Animation{
		0% {
			width: 0;
			right: 46px;
			top: 54px;
		}
		65% {
			width: 0;
			right: 46px;
			top: 54px; 
		}
		84% {
			width: 55px;
			right: 0px;
			top: 35px; 
		}
		100% {
			width: 47px;
			right: 8px;
			top: 38px; 
		} 
	}
	@keyframes success_long_Animation{
		0% {
			width: 0;
			right: 46px;
			top: 54px;
		}
		65% {
			width: 0;
			right: 46px;
			top: 54px; 
		}
		84% {
			width: 55px;
			right: 0px;
			top: 35px; 
		}
		100% {
			width: 47px;
			right: 8px;
			top: 38px; 
		} 
	}
/*success_Animation*/
 
/* ---------------------------------------------------------------------------------------------------------------------------- */
 
/*lose_Animation*/
	@-webkit-keyframes lose_Animation{
		0%{
			-webkit-transform:scale(0.6);
			opacity:0.2;
		}
		50% {
			-webkit-transform:scale(0.6);
			opacity:0.5;
		}
		80% {
			-webkit-transform:scale(1.15);
			opacity:0.8;
		}
		100% {
			-webkit-transform:scale(1);
			opacity:1.0;
		} 
	}
	@keyframes lose_Animation{
		0%{
			transform:scale(0.6);
			opacity:0.2;
		}
		50% {
			transform:scale(0.6);
			opacity:0.5;
		}
		80% {
			transform:scale(1.15);
			opacity:0.8;
		}
		100% {
			transform:scale(1);
			opacity:1.0;
		} 
	}
/*lose_Animation*/
 
 
/*load_Animation*/
	@keyframes load_Animation{
		0%{
			transform:scale(0.6);
			opacity:0.2;
		}
		50% {
			transform:scale(0.6);
			opacity:0.5;
		}
		80% {
			transform:scale(1.15);
			opacity:0.8;
		}
		100% {
			transform:scale(1);
			opacity:1.0;
		} 
	}
	@-webkit-keyframes load_Animation{
		0%{
			-webkit-transform:scale(0.6);
			opacity:0.2;
		}
		50% {
			-webkit-transform:scale(0.6);
			opacity:0.5;
		}
		80% {
			-webkit-transform:scale(1.15);
			opacity:0.8;
		}
		100% {
			-webkit-transform:scale(1);
			opacity:1.0;
		} 
	}
/*load_Animation*/
.load{
	position: relative;
	width: 60px;
	height: 80px;
	-webkit-border-radius:50px;
	border-radius: 50px;
	border: 4px solid #fff;
	margin: 15px auto 5px;
	top: 10px;
}
.load .icon_box{
	margin:10px auto;
	width:60px;
	height:60px;	
}
.load .cirBox1,.load .cirBox2,.load .cirBox3{
	width:60px; 
	height:60px;
	position:absolute;
	left:0;
	top:0;
}
.load .cirBox1 > div,.load .cirBox2 > div,.load .cirBox3 > div{
	width:10px;
	height:10px;
	-webkit-border-radius:100%;
	border-radius:100%;
	background-color:#ccc;
	position:absolute;
}
.load  .cirBox1{
	-webkit-transform:rotate(30deg);
	transform:rotate(30deg);
}
.load  .cirBox2{
	-webkit-transform:rotate(60deg);
	transform:rotate(60deg);
}
.load  .cirBox3{ 
	-webkit-transform:rotate(90deg);
	transform:rotate(90deg);
}
.load .cir1{
	left:0;
	top:0;
}
.load .cir2{
	right:0;
	top:0;
}
.load .cir3{
	right:0;
	bottom:0;
}
.load .cir4{
	left:0;
	bottom:0;
}
.load .cir1,.load .cir2,.load .cir3,.load .cir4{
	-webkit-animation:cir_Animation 1.2s ease 0 infinite;
	animation:cir_Animation 1.2s ease 0 infinite;
}
.cirBox1 .cir2{
	-webkit-animation-delay:-1.1s;
	animation-delay:-1.1s;
}
.cirBox1 .cir3{
	-webkit-animation-delay:-0.8s;
	animation-delay:-0.8s;
}
.cirBox1 .cir4{
	-webkit-animation-delay:-0.5s;
	animation-delay:-0.5s;
}
 
.cirBox2 .cir2{
	-webkit-animation-delay:-1.0s;
	animation-delay:-1.0s;
}
.cirBox2 .cir3{
	-webkit-animation-delay:-0.7s;
	animation-delay:-0.7s;
}
.cirBox2 .cir4{
	-webkit-animation-delay:-0.4s;
	animation-delay:-0.4s;
}
 
.cirBox3 .cir2{
	-webkit-animation-delay:-0.9s;
	animation-delay:-0.9s;
}
.cirBox3 .cir3{
	-webkit-animation-delay:-0.6s;
	animation-delay:-0.6s;
}
.cirBox3 .cir4{
	-webkit-animation-delay:-0.3s;
	animation-delay:-0.3s;
}
 
 
@keyframes cir_Animation{
	0%,80%,100%{
		-webkit-transform:scale(0.4);
		transform:scale(0.4);
	}
	40%{
		-webkit-transform:scale(1.0);
		transform:scale(1.0);
	}
}
@-webkit-keyframes cir_Animation{
	0%,80%,100%{
		-webkit-transform:scale(0.4);
		transform:scale(0.4);
	}
	40%{
		-webkit-transform:scale(1.0);
		transform:scale(1.0);
	}
}
.mask{
	width:100%;
	height:100%;
	background-color:rgba(0,0,0,0.5);
	position: fixed;
	left: 0;
	top: 0;
	z-index:1000;
}
</style>
</head> 
<body>
<div class="btn-container">
	<input type="button" id="success" value="成功" />
	<input type="button" id="error" value="错误" />
	<input type="button" id="load" value="正在加载" />
	<input type="button" id="tip" value="提示" />
</div>
</body>
</html>
<script src="http://libs.baidu.com/zepto/0.8/zepto.min.js"></script>
<script>
/*
 * @弹出提示层 ( 加载动画(load), 提示动画(tip), 成功(success), 错误(error), )
 * @method  tipBox
 * @description 默认配置参数 
 * @param {Number} width -宽度
 * @param {Number} height -高度		
 * @param {String} str -默认文字
 * @param {Object} windowDom -载入窗口 默认当前窗口
 * @param {Number} setTime -定时消失(毫秒) 默认为0 不消失
 * @param {Boolean} hasMask -是否显示遮罩
 * @param {Boolean} hasMaskWhite -显示白色遮罩 
 * @param {Boolean} clickDomCancel -点击空白取消
 * @param {Function} callBack -回调函数 (只在开启定时消失时才生效)
 * @param {String} type -动画类型 (加载,成功,失败,提示)
 * @example 
 * new TipBox(); 
 * new TipBox({type:'load',setTime:1000,callBack:function(){ alert(..) }}); 
*/
function TipBox(cfg){
	this.config = { 
		width          : 180,    
		height         : 150,    			
		str            : '正在处理',     
		windowDom      : window, 
		setTime        : 0,   
		hasMask        : true,  
		hasMaskWhite   : false, 
		clickDomCancel : false,  
		callBack       : null,
		type           : 'success'
	}
	$.extend(this.config,cfg);	
 
	//存在就retrun
	if(TipBox.prototype.boundingBox) return;
 
	//初始化
	this.render(this.config.type);	
	return this; 
};
 
//外层box
TipBox.prototype.boundingBox = null;
 
//渲染
TipBox.prototype.render = function(tipType,container){	
	this.renderUI(tipType); 
 
	//绑定事件
	this.bindUI(); 
 
	//初始化UI
	this.syncUI(); 
	$(container || this.config.windowDom.document.body).append(TipBox.prototype.boundingBox);	
};
 
//渲染UI
TipBox.prototype.renderUI = function(tipType){ 
	TipBox.prototype.boundingBox = $("<div id='animationTipBox'></div>");  		
	tipType == 'load'    && this.loadRenderUI();
	tipType == 'success' && this.successRenderUI();	
	tipType == 'error'   && this.errorRenderUI();
	tipType == 'tip'     && this.tipRenderUI();
	TipBox.prototype.boundingBox.appendTo(this.config.windowDom.document.body);
 
	//是否显示遮罩
	if(this.config.hasMask){
		this.config.hasMaskWhite ? this._mask = $("<div class='mask_white'></div>") : this._mask = $("<div class='mask'></div>");
		this._mask.appendTo(this.config.windowDom.document.body);
	}	
 
	//定时消失
	_this = this;
	!this.config.setTime && typeof this.config.callBack === "function" && (this.config.setTime = 1);	
	this.config.setTime && setTimeout( function(){ _this.close(); }, _this.config.setTime );
};
 
TipBox.prototype.bindUI = function(){
	_this = this;			
 
	//点击空白立即取消
	this.config.clickDomCancel && this._mask && this._mask.click(function(){_this.close();});						
};
TipBox.prototype.syncUI = function(){ 			
	TipBox.prototype.boundingBox.css({
		width       : this.config.width+'px',
		height      : this.config.height+'px',
		marginLeft  : "-"+(this.config.width/2)+'px',
		marginTop   : "-"+(this.config.height/2)+'px'
	});	
};
 
//提示效果UI
TipBox.prototype.tipRenderUI = function(){
	var tip = "<div class='tip'>";
		tip +="		<div class='icon'>i</div>";
		tip +="		<div class='dec_txt'>"+this.config.str+"</div>";
		tip += "</div>";
	TipBox.prototype.boundingBox.append(tip);
};
 
//成功效果UI
TipBox.prototype.successRenderUI = function(){
	var suc = "<div class='success'>";
		suc +="	<div class='icon'>";
		suc +=		"<div class='line_short'></div>";
		suc +=		"<div class='line_long'></div>	";		
		suc +=  "</div>";
		suc +="	<div class='dec_txt'>"+this.config.str+"</div>";
		suc += "</div>";
	TipBox.prototype.boundingBox.append(suc);
};
 
//错误效果UI
TipBox.prototype.errorRenderUI = function(){
	var err  = "<div class='lose'>";
		err +=	"	<div class='icon'>";
		err +=	"		<div class='icon_box'>";
		err +=	"			<div class='line_left'></div>";
		err +=	"			<div class='line_right'></div>";
		err +=	"		</div>";
		err +=	"	</div>";
		err +=	"<div class='dec_txt'>"+this.config.str+"</div>";
		err +=	"</div>";
	TipBox.prototype.boundingBox.append(err);
};
 
//加载动画load UI
TipBox.prototype.loadRenderUI = function(){
	var load = "<div class='load'>";
		load += "<div class='icon_box'>";
	for(var i = 1; i < 4; i++ ){
		load += "<div class='cirBox"+i+"'>";
		load += 	"<div class='cir1'></div>";
		load += 	"<div class='cir2'></div>";
		load += 	"<div class='cir3'></div>";
		load += 	"<div class='cir4'></div>";
		load += "</div>";
	}
	load += "</div>";
	load += "</div>";
	load += "<div class='dec_txt'>"+this.config.str+"</div>";
	TipBox.prototype.boundingBox.append(load);
};
 
//关闭
TipBox.prototype.close = function(){	
	TipBox.prototype.destroy();
	this.destroy();
	this.config.setTime && typeof this.config.callBack === "function" && this.config.callBack();				
};
 
//销毁
TipBox.prototype.destroy = function(){
	this._mask && this._mask.remove();
	TipBox.prototype.boundingBox && TipBox.prototype.boundingBox.remove(); 
	TipBox.prototype.boundingBox = null;
};
</script>
<script>
	//成功
	$("#success").click(function(){
		new TipBox({type:'success',str:'操作成功',setTime:1500});
	});
	//错误
	$("#error").click(function(){
		new TipBox({type:'error',str:'对不起,出错了!',setTime:1500});
	});
 
	//提示
	$("#tip").click(function(){
		new TipBox({type:'tip',str:'这是提示信息',setTime:1500});
	});
 
	//加载
	$("#load").click(function(){
		new TipBox({type:'load',str:"努力加载中...",setTime:1500,callBack:function(){
			new TipBox({type:'success',str:'操作成功',setTime:1500});
		}});
	});
 
</script>

本文标题:js+css3之移动端消息框

本文地址:http://www.hehaibao.com/js-css3-message-box/

版权声明:本博客所有文章除特别声明外,均采用 署名-非商业性使用-禁止演绎 4.0 国际 许可协议。请尊重他人的劳动成果!

一条评论
  • 嘻嘻 - Windows 7  |  Chrome 61.0.3163.79

    2017-09-16 23:29

    超实用,项目赶进度,我就先抄走了,拜谢大神。


发表评论

电子邮件地址不会被公开。 必填项已用*标注