-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoastFull.js
160 lines (151 loc) · 5.1 KB
/
toastFull.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
/**
*
* @cchen
* @date 2017-02-25
* @version 1.0
* @plugin Toast
*/
;(function(root,factory){
root.toastFull = factory();
})(window,function(){
var toastFull = function(options){
this.opt = {};
this.opacity=0;
this.raf='';
this.st='';
this.status = 0;
this.loadingEle = null;
this.winH = document.body.clientHeight/2-20;
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
this.extend(options);
};
toastFull.prototype = {
extend: function(options){
var defaults = {
textColor: '#fff',
timer: 2000 //toast停留时间
};
var key;
for(key in options){
var defaultVal = defaults[key];
var optionVal = options[key];
if(optionVal == defaultVal){
continue;
}
else if(optionVal !== undefined){
defaults[key] = optionVal;
}
}
this.opt = defaults;
},
fadeIn:function(newToast,type){
var _this = this;
_this.opacity+=.1;
newToast.style.opacity = _this.opacity;
if(_this.opacity<1 && _this.status==0 ){
_this.raf = requestAnimationFrame(function(){
_this.fadeIn(newToast,type);
});
}
else{
cancelAnimationFrame(_this.raf);
if(type == 1){
_this.st = setTimeout(function(){
_this.raf = _this.fadeOut(newToast);
},_this.opt.timer);
}
}
},
fadeOut:function(newToast){
var _this = this;
_this.opacity-=.1;
_this.status = 1;
newToast.style.opacity = _this.opacity.toFixed(2);
if(_this.opacity>0){
requestAnimationFrame(function(){
_this.raf = _this.fadeOut(newToast);
});
}
else{
cancelAnimationFrame(_this.raf);
newToast.style.zIndex = -99;
}
},
createEntity: function(msg,timer,type){
var _this = this;
var idNum = Math.random().toString().replace('.', '');
var toastId = 'toast'+idNum;
var body = document.getElementsByTagName('body')[0];
var newToast = document.createElement("div");
var posY='0';
_this.status = 0;
if(typeof(timer) == 'number'){
this.opt.timer = timer;
}
if(type ==1){
var box = document.createElement("div");
box.setAttribute('style','position:absolute;top:50%;left:50%;padding:10px;max-width:280px;border-radius:5px;text-align:center;background-color:rgba(0,0,0,0.7);webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);');
box.innerHTML = '<img src="images/toast-error.png" style="margin:5px auto;display:block;width:35px;">'+msg;
newToast.appendChild(box);
}
else if(type == 2){
var box = document.createElement("div");
box.setAttribute('style','position:absolute;top:50%;left:50%;padding:10px 15px;max-width:280px;border-radius:5px;text-align:center;background-color:rgba(0,0,0,0.7);webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);');
box.innerHTML = '<img src="images/hourglass.svg" style="margin:5px auto;display:block;width:35px;">'+msg;
newToast.appendChild(box);
}
newToast.id = toastId; //设置id
newToast.className = 'cToast';
newToast.setAttribute('style','position:fixed;top:0;left:0;font-size:14px;background-color:rgba(0,0,0,0);color:'+_this.opt.textColor+';opacity:0;width:100%;height:100%;text-align:center;z-index:1000;');
body.appendChild(newToast);
return newToast;
},
alert: function(msg,timer){
var _this = this;
if(document.getElementsByClassName('cToast').length>0){
var someToasts = document.getElementsByClassName('cToast');
someToasts[0].parentNode.removeChild(someToasts[0]);
cancelAnimationFrame(_this.raf);
clearTimeout(_this.st);
_this.opacity = 0;
}
var newToast = _this.createEntity(msg,timer,1);
requestAnimationFrame(function(){
_this.raf = _this.fadeIn(newToast,1);
});
},
loadStart: function(msg){
var _this = this;
if(document.getElementsByClassName('cToast').length>0){
var someToasts = document.getElementsByClassName('cToast');
someToasts[0].parentNode.removeChild(someToasts[0]);
cancelAnimationFrame(_this.raf);
clearTimeout(_this.st);
_this.opacity = 0;
}
var newToast = _this.createEntity(msg,'',2);
requestAnimationFrame(function(){
_this.raf = _this.fadeIn(newToast,2);
});
_this.loadingEle = newToast;
},
loadFinishSuccess: function(msg){
var _this = this;
_this.loadingEle.style.left = '0';
_this.loadingEle.firstChild.innerHTML = '<img src="images/toast-success.png" style="margin:5px auto;display:block;width:35px;">'+msg;
setTimeout(function(){
_this.raf = _this.fadeOut(_this.loadingEle);
},1000);
},
loadFinishError: function(msg){
var _this = this;
_this.loadingEle.style.left = '0';
_this.loadingEle.firstChild.innerHTML = '<img src="images/toast-error.png" style="margin:5px auto;display:block;width:35px;">'+msg;
setTimeout(function(){
_this.raf = _this.fadeOut(_this.loadingEle);
},1000);
}
};
return toastFull;
});