2014.01.12 笔记 (很好的提示框)
美观并且可以自定义按钮文字,可直接带回调函数的提示框
首先是核心JS代码。 这个代码还在理解中。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 (function(e, t) { 2 "use strict"; 3 var n = e.document, 4 alertifyFun; 5 alertifyFun = function() { 6 var findDom, i, s, o, u, a, f, l, c, h, p, d, v, m, HtmlObj, showTime, b, isAlt, KeyCodes, BtnTexts, x, T; 7 return HtmlObj = { 8 buttons: { 9 holder: '<nav class="alertify-buttons">{{buttons}}</nav>', 10 ok: '<a href="#" class="alertify-button alertify-button-ok" id="aOK">{{ok}}</a>', 11 cancel: '<a href="#" class="alertify-button alertify-button-cancel" id="aCancel">{{cancel}}</a>' 12 }, 13 input: '<input type="text" class="alertify-text" id="aText">', 14 message: '<p class="alertify-message">{{message}}</p>', 15 log: '<article class="alertify-log{{class}}">{{message}}</article>' 16 }, 17 showTime = 5e3,//右下角“点击按钮的结果的弹出框” 的默认显示时间 18 KeyCodes = { 19 ENTER: 13, 20 ESC: 27 21 }, 22 BtnTexts = { 23 ok: "OK", 24 cancel: "Cancel" 25 }, 26 T = [],//T.push({type: 弹框类型a,message: 提示内容b,callback: 回调函数c}) 27 isAlt = !1,//记录点击a标签时,是否已经弹出提示框 28 findDom = function(e) { 29 return n.getElementById(e) 30 }, 31 i = function(i) { 32 var o = findDom("aOK") || t, 33 u = findDom("aCancel") || t, 34 f = findDom("aText") || t, 35 l = typeof o != "undefined", 36 c = typeof u != "undefined", 37 p = "", 38 d, 39 v, 40 m, 41 g; 42 d = function(e) { 43 m(e), 44 typeof f != "undefined" && (p = f.value), 45 typeof i == "function" && i(!0, p), 46 typeof e.preventDefault != "undefined" && e.preventDefault() 47 }, 48 v = function(e) { 49 m(e), 50 typeof i == "function" && i(!1), 51 typeof e.preventDefault != "undefined" && e.preventDefault() 52 }, 53 m = function(e) { 54 a(), 55 h(n.body, "keyup", g) 56 }, 57 g = function(e) { 58 var t = e.keyCode; 59 t === KeyCodes.ESC && c && v(e) 60 }, 61 l && s(o, "click", d), 62 c && s(u, "click", v), 63 s(n.body, "keyup", g), 64 e.setTimeout(function() { 65 f ? f.focus() : o.focus() 66 }, 67 50) 68 }, 69 s = function(e, t, n) { 70 typeof e.addEventListener == "function" ? e.addEventListener(t, n, !1) : e.attachEvent && e.attachEvent("on" + t, n) 71 }, 72 //此处判断生成提示框中的内容 73 o = function(e) { 74 var t = "", 75 n = e.type, 76 r = e.message; 77 t += '<div class="alertify-dialog">', 78 t += '<article class="alertify-inner">', 79 t += HtmlObj.message.replace("{{message}}", r), 80 n === "prompt" && (t += HtmlObj.input), 81 t += HtmlObj.buttons.holder, 82 t += "</article>", 83 t += "</div>"; 84 switch (n) { 85 case "confirm": 86 case "prompt": 87 t = t.replace("{{buttons}}", HtmlObj.buttons.ok + HtmlObj.buttons.cancel), 88 t = t.replace("{{ok}}", BtnTexts.ok).replace("{{cancel}}", BtnTexts.cancel); 89 break; 90 case "alert": 91 t = t.replace("{{buttons}}", HtmlObj.buttons.ok), 92 t = t.replace("{{ok}}", BtnTexts.ok); 93 break; 94 default: 95 } 96 return b.className = "alertify alertify-show alertify-" + n, 97 m.className = "alertify-cover", 98 t 99 }, 100 //此处按时去除“点击按钮的结果的弹出框” 101 u = function() { 102 setTimeout(function() { 103 var e = x.childNodes[x.childNodes.length - 1]; 104 typeof e != "undefined" && x.removeChild(e) 105 }, 106 showTime) 107 }, 108 a = function() { 109 T.splice(0, 1), 110 T.length > 0 ? c() : (isAlt = !1, b.className = "alertify alertify-hide alertify-hidden", m.className = "alertify-cover alertify-hidden") 111 }, 112 //此处... 113 f = function() { 114 n.createElement("nav"), 115 n.createElement("article"), 116 n.createElement("section"), 117 118 m = n.createElement("div"), 119 m.setAttribute("id", "alertifycover"), 120 m.className = "alertify-cover alertify-hidden", 121 n.body.appendChild(m),// 122 123 b = n.createElement("section"), 124 b.setAttribute("id", "alertify"), 125 b.className = "alertify alertify-hidden", 126 n.body.appendChild(b), 127 128 x = n.createElement("section"), 129 x.setAttribute("id", "alertifylogs"), 130 x.className = "alertify-logs", 131 n.body.appendChild(x), 132 133 delete this.init 134 }, 135 //此处生成右下角的“点击按钮的结果的弹出框” 中的内容 136 l = function(e, t) { 137 var r = n.createElement("article"); 138 r.className = "alertify-log" + (typeof t == "string" && t !== "" ? " alertify-log-" + t: ""), 139 r.innerHTML = e, 140 x.insertBefore(r, x.firstChild), 141 setTimeout(function() { 142 r.className = r.className + " alertify-log-show" 143 },50),//显示弹出框 144 u()//按时去除 145 }, 146 c = function() { 147 var e = T[0]; 148 isAlt = !0, 149 b.innerHTML = o(e),//调用方法向提示框中插入要显示内容 150 i(e.callback)//点击确定或者取消后的回调方法 151 }, 152 h = function(e, t, n) { 153 typeof e.removeEventListener == "function" ? e.removeEventListener(t, n, !1) : e.detachEvent && e.detachEvent("on" + t, n) 154 }, 155 //根据点击 a 标签的类型,来执行相应的方法 156 p = function(e, t, n) { 157 var r = function() { 158 if (b && b.scrollTop !== null) return;//若右下角显示的弹出框数量过多,则不再弹框 159 r() 160 }; 161 if (typeof e != "string") throw new Error("message must be a string"); 162 if (typeof t != "string") throw new Error("type must be a string"); 163 if (typeof n != "undefined" && typeof n != "function") throw new Error("fn must be a function"); 164 return typeof this.init == "function" && (this.init(), r()), 165 T.push({ 166 type: t, 167 message: e, 168 callback: n 169 }), 170 isAlt || c(), 171 this 172 }, 173 d = function(e) { 174 return function(t) { 175 v(t, e) 176 } 177 }, 178 v = function(e, t) { 179 var n = function() { 180 if (x && x.scrollTop !== null) return; 181 n() 182 }; 183 return typeof this.init == "function" && (this.init(), n()), 184 l(e, t), 185 this 186 }, 187 { 188 alert: function(e, t) { 189 return p.call(this, e, "alert", t),//e:message; "alert": type; t:function() 190 this 191 }, 192 confirm: function(e, t) { 193 return p.call(this, e, "confirm", t), 194 this 195 }, 196 extend: d, 197 init: f, 198 log: function(e, t) { 199 return v.call(this, e, t), 200 this 201 }, 202 prompt: function(e, t) { 203 return p.call(this, e, "prompt", t), 204 this 205 }, 206 success: function(e) { 207 return v.call(this, e, "success"), 208 this 209 }, 210 error: function(e) { 211 return v.call(this, e, "error"), 212 this 213 }, 214 delay: showTime, 215 labels: BtnTexts 216 } 217 }, 218 typeof define == "function" ? define([], 219 function() { 220 return new alertifyFun 221 }) : typeof e.alertify == "undefined" && (e.alertify = new alertifyFun) 222 })(this);
然后是界面调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 3 <title>JavaScript消息提示框类库 – Alertify 演示页面 >> 给力技术</title> 4 <!--演示页面的CSS样式--> 5 6 <link rel="stylesheet" href="AlertifyPage_files/alertify.core-default.css"> 7 8 <script> 9 // 使低版本浏览器支持html5 10 document.createElement("nav"); 11 document.createElement("header"); 12 document.createElement("article"); 13 document.createElement("section"); 14 </script> 15 16 </head> 17 <body style=""> 18 <div id="header"> 19 JavaScript消息提示框类库 - Alertify 20 </div><p></p><p></p> 21 <div class="content"> 22 <h2><b>JavaScript消息提示框类库 – Alertify 演示页面</b></h2> 23 24 25 <p></p> <p></p> 26 27 28 <h3><span>自定义按钮文字</span></h3> 29 <p> 30 <label for="okVal">自定义确定按钮的文字: </label><input type="text" id="okVal" value="确定"> 31 <label for="cancelVal">自定义取消按钮的文字: </label><input type="text" id="cancelVal" value="取消"> 32 </p> 33 <nav> 34 <a class="button-primary" href="javascript:void(0);" id="alert2" onClick="lAlert2Click('Alertify 演示页面');">Alert Dialog</a> 35 <a class="button-primary" href="javascript:void(0);" id="confirm2" onClick="lConfirm2Click('Alertify 演示页面');">Confirm Dialog</a> 36 <a class="button-primary" href="javascript:void(0);" id="prompt2" onClick="lPrompt2Click('Alertify 演示页面');">Prompt Dialog</a> 37 </nav> 38 39 <p></p> <p></p> 40 41 42 <h3><span>自定义提示信息</span></h3> 43 <nav> 44 <a class="button-primary" href="javascript:void(0);" id="log1" onClick="log1Click();">Notification</a> 45 <a class="button-primary" href="javascript:void(0);" id="log2" onClick="log2Click();">Success</a> 46 <a class="button-primary" href="javascript:void(0);" id="log3" onClick="log3Click();">Error</a> 47 <p></p> 48 49 </nav> 50 <p></p> <p></p> 51 52 <script type="text/javascript" src="./AlertifyPage_files/alertify.min.js"></script> 53 <script type="text/javascript"> 54 55 var ok = document.getElementById("okVal"),//确定按钮的文字 56 cancel = document.getElementById("cancelVal");//取消按钮的文字 57 58 var setLabel = function (reset) { 59 reset = reset || false; 60 alertify.labels.ok = (ok.value && !reset) ? ok.value : "OK"; 61 alertify.labels.cancel = (cancel.value && !reset) ? cancel.value : "Cancel"; 62 }; 63 64 65 var lAlert2Click = function (message) { 66 setLabel(); 67 alertify.alert(message); return false; 68 }; 69 70 var lConfirm2Click = function (message) { 71 setLabel(); 72 alertify.confirm(message, function (e) { 73 if (e) { 74 alertify.success("你点击的是 '" + alertify.labels.ok + "'"); 75 } else { 76 alertify.error("你点击的是 '" + alertify.labels.cancel + "'"); 77 } 78 }); 79 return false; 80 }; 81 82 var lPrompt2Click= function (message) { 83 setLabel(); 84 alertify.prompt(message, function (e, str) { 85 if (e) { 86 alertify.success("你点击的是 '" + alertify.labels.ok + "' ,输入的文字是: " + str); 87 } else { 88 alertify.error("你点击的是 '" + alertify.labels.cancel + "'"); 89 } 90 }); 91 return false; 92 }; 93 94 var log1Click = function () { alertify.log("正常提示消息."); return false; }; 95 var log2Click = function () { alertify.success("success 提示消息."); return false; }; 96 var log3Click = function () { alertify.error("error 提示消息."); return false; }; 97 98 </script> 99 </div> 100 101 </body></html>
这个是提示框的样式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@charset "utf-8"; /* CSS Document */ .alertify-show,.alertify-log{-webkit-transition:all 500ms cubic-bezier(0.175,0.885,0.320,1);-webkit-transition:all 500ms cubic-bezier(0.175,0.885,0.320,1.275);-moz-transition:all 500ms cubic-bezier(0.175,0.885,0.320,1.275);-ms-transition:all 500ms cubic-bezier(0.175,0.885,0.320,1.275);-o-transition:all 500ms cubic-bezier(0.175,0.885,0.320,1.275);transition:all 500ms cubic-bezier(0.175,0.885,0.320,1.275);}.alertify-hide{-webkit-transition:all 250ms cubic-bezier(0.600,0,0.735,0.045);-webkit-transition:all 250ms cubic-bezier(0.600,-0.280,0.735,0.045);-moz-transition:all 250ms cubic-bezier(0.600,-0.280,0.735,0.045);-ms-transition:all 250ms cubic-bezier(0.600,-0.280,0.735,0.045);-o-transition:all 250ms cubic-bezier(0.600,-0.280,0.735,0.045);transition:all 250ms cubic-bezier(0.600,-0.280,0.735,0.045);}.alertify-cover{position:fixed;z-index:99999;top:0;right:0;bottom:0;left:0;}.alertify{position:fixed;z-index:99999;top:50px;left:50%;width:550px;margin-left:-275px;}.alertify-hidden{top:-50px;visibility:hidden;}.alertify-logs{position:fixed;z-index:5000;bottom:10px;right:10px;width:300px;}.alertify-log{display:block;margin-top:10px;position:relative;right:-300px;}.alertify-log-show{right:0;}.alertify-dialog{padding:25px;}.alertify-inner{text-align:center;}.alertify-text{margin-bottom:15px;width:100%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;font-size:100%;}.alertify-buttons{}.alertify-button{display:inline-block;cursor:pointer;margin-left:5px;}@media only screen and (max-width:680px){.alertify,.alertify-logs{width:90%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}.alertify{left:5%;margin:0;}} .alertify,.alertify-log{font-family:sans-serif;}.alertify{background:#FFF;border:10px solid #333;border:10px solid rgba(0,0,0,.7);border-radius:8px;box-shadow:0 3px 3px rgba(0,0,0,.3);-webkit-background-clip:padding;-moz-background-clip:padding;background-clip:padding-box;}.alertify-text{border:1px solid #CCC;padding:10px;border-radius:4px;}.alertify-button{border-radius:4px;color:#FFF;font-weight:bold;padding:6px 15px;text-decoration:none;text-shadow:1px 1px 0 rgba(0,0,0,.5);box-shadow:inset 0 1px 0 0 rgba(255,255,255,.5);background-image:-webkit-linear-gradient(top,rgba(255,255,255,.3),rgba(255,255,255,0));background-image:-moz-linear-gradient(top,rgba(255,255,255,.3),rgba(255,255,255,0));background-image:-ms-linear-gradient(top,rgba(255,255,255,.3),rgba(255,255,255,0));background-image:-o-linear-gradient(top,rgba(255,255,255,.3),rgba(255,255,255,0));background-image:linear-gradient(top,rgba(255,255,255,.3),rgba(255,255,255,0));}.alertify-button:hover,.alertify-button:focus{outline:none;box-shadow:0 0 15px #2B72D5;background-image:-webkit-linear-gradient(top,rgba(0,0,0,.1),rgba(0,0,0,0));background-image:-moz-linear-gradient(top,rgba(0,0,0,.1),rgba(0,0,0,0));background-image:-ms-linear-gradient(top,rgba(0,0,0,.1),rgba(0,0,0,0));background-image:-o-linear-gradient(top,rgba(0,0,0,.1),rgba(0,0,0,0));background-image:linear-gradient(top,rgba(0,0,0,.1),rgba(0,0,0,0));}.alertify-button:active{position:relative;top:1px;}.alertify-button-cancel{background-color:#FE1A00;border:1px solid #D83526;}.alertify-button-ok{background-color:#5CB811;border:1px solid #3B7808;}.alertify-log{background:#1F1F1F;background:rgba(0,0,0,.9);padding:15px;border-radius:4px;color:#FFF;text-shadow:-1px -1px 0 rgba(0,0,0,.5);}.alertify-log-error{background:#FE1A00;background:rgba(254,26,0,.9);}.alertify-log-success{background:#5CB811;background:rgba(92,184,17,.9);}
开始是觉得这个代码写的好复杂,完全可以写的简单些,后来发现,它还处理了一些我没有考虑到的细节地方。
在这个JS中,看到了这个方法: p.call(thisObj,arg1,arg2,arg3); 没有用过,不太明白,然后查了一下资料。
通过人家写的方法,让我很快明白了。他是这么写的。
通过简单的示例来说明:
obj1.method1.call(obj2,argument1,argument2)
如上,call的作用就是把obj1的方法放到obj2上使用,后面的argument1..这些做为参数传入.
举一个具体的例子
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);
这个例子中的意思就是用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4); // 注意:js 中的函数其实是对象,函数名是对 Function 对象的引用。
看一个稍微复杂一点的例子
function Class1()
{
this.name = "class1";
this.showNam = function()
{
alert(this.name);
}
}
function Class2()
{
this.name = "class2";
}
var c1 = new Class1();
var c2 = new Class2();
c1.showNam.call(c2);
注意,call 的意思是把 c1 的方法放到c2上执行,原来c2是没有showNam() 方法,现在是把c1 的showNam()方法放到 c2 上来执行,所以this.name 应该是 class2,执行的结果就是 :alert("class2");
怎么样,觉得有意思了吧,可以让a对象来执行b对象的方法,这是java程序员所不敢想的。还有更有趣的,可以用 call 来实现继承
function Class1()
{
this.showTxt = function(txt)
{
alert(txt);
}
}
function Class2()
{
Class1.call(this);
}
var c2 = new Class2();
c2.showTxt("cc");
这样 Class2 就继承Class1了,Class1.call(this) 的 意思就是使用 Class1 对象代替this对象,那么 Class2 中不就有Class1 的所有属性和方法了吗,c2 对象就能够直接调用Class1 的方法以及属性了,执行结果就是:alert(“cc”);
对的,就是这样,这就是 javaScript 如何来模拟面向对象中的继承的,还可以实现多重继承。
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
很简单,使用两个 call 就实现多重继承了
当然,js的继承还有其他方法,例如使用原型链,这个不属于本文的范畴,只是在此说明call 的用法
说了call ,当然还有 apply,这两个方法基本上是一个意思
区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组