xcConfirm.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * 使用说明:
  3. * window.wxc.Pop(popHtml, [type], [options])
  4. * popHtml:html字符串
  5. * type:window.wxc.xcConfirm.typeEnum集合中的元素
  6. * options:扩展对象
  7. * 用法:
  8. * 1. window.wxc.xcConfirm("我是弹窗<span>lalala</span>");
  9. * 2. window.wxc.xcConfirm("成功","success");
  10. * 3. window.wxc.xcConfirm("请输入","input",{onOk:function(){}})
  11. * 4. window.wxc.xcConfirm("自定义",{title:"自定义"})
  12. */
  13. (function($){
  14. window.wxc = window.wxc || {};
  15. window.wxc.xcConfirm = function(popHtml, type, options) {
  16. var btnType = window.wxc.xcConfirm.btnEnum;
  17. var eventType = window.wxc.xcConfirm.eventEnum;
  18. var popType = {
  19. info: {
  20. title: "信息",
  21. icon: "0 0",//蓝色i
  22. btn: btnType.ok
  23. },
  24. success: {
  25. title: "成功",
  26. icon: "0 -48px",//绿色对勾
  27. btn: btnType.ok
  28. },
  29. error: {
  30. title: "错误",
  31. icon: "-48px -48px",//红色叉
  32. btn: btnType.ok
  33. },
  34. confirm: {
  35. title: "提示",
  36. icon: "-48px 0",//黄色问号
  37. btn: btnType.okcancel
  38. },
  39. warning: {
  40. title: "警告",
  41. icon: "0 -96px",//黄色叹号
  42. btn: btnType.okcancel
  43. },
  44. input: {
  45. title: "输入",
  46. icon: "",
  47. btn: btnType.ok
  48. },
  49. custom: {
  50. title: "",
  51. icon: "",
  52. btn: btnType.ok
  53. }
  54. };
  55. var itype = type ? type instanceof Object ? type : popType[type] || {} : {};//格式化输入的参数:弹窗类型
  56. var config = $.extend(true, {
  57. //属性
  58. title: "", //自定义的标题
  59. icon: "", //图标
  60. btn: btnType.ok, //按钮,默认单按钮
  61. //事件
  62. onOk: $.noop,//点击确定的按钮回调
  63. onCancel: $.noop,//点击取消的按钮回调
  64. onClose: $.noop//弹窗关闭的回调,返回触发事件
  65. }, itype, options);
  66. var $txt = $("<p>").html(popHtml);//弹窗文本dom
  67. var $tt = $("<span>").addClass("tt").text(config.title);//标题
  68. var icon = config.icon;
  69. var $icon = icon ? $("<div>").addClass("bigIcon").css("backgroundPosition",icon) : "";
  70. var btn = config.btn;//按钮组生成参数
  71. var popId = creatPopId();//弹窗索引
  72. var $box = $("<div>").addClass("xcConfirm");//弹窗插件容器
  73. var $layer = $("<div>").addClass("xc_layer");//遮罩层
  74. var $popBox = $("<div>").addClass("popBox");//弹窗盒子
  75. var $ttBox = $("<div>").addClass("ttBox");//弹窗顶部区域
  76. var $txtBox = $("<div>").addClass("txtBox");//弹窗内容主体区
  77. var $btnArea = $("<div>").addClass("btnArea");//按钮区域
  78. var $ok = $("<a>").addClass("sgBtn").addClass("ok").text("确定");//确定按钮
  79. var $cancel = $("<a>").addClass("sgBtn").addClass("cancel").text("取消");//取消按钮
  80. var $input = $("<input>").addClass("inputBox");//输入框
  81. var $clsBtn = $("<a>").addClass("clsBtn");//关闭按钮
  82. //建立按钮映射关系
  83. var btns = {
  84. ok: $ok,
  85. cancel: $cancel
  86. };
  87. init();
  88. function init(){
  89. //处理特殊类型input
  90. if(popType["input"] === itype){
  91. $txt.append($input);
  92. }
  93. creatDom();
  94. bind();
  95. }
  96. function creatDom(){
  97. $popBox.append(
  98. $ttBox.append(
  99. $clsBtn
  100. ).append(
  101. $tt
  102. )
  103. ).append(
  104. $txtBox.append($icon).append($txt)
  105. ).append(
  106. $btnArea.append(creatBtnGroup(btn))
  107. );
  108. $box.attr("id", popId).append($layer).append($popBox);
  109. $("body").append($box);
  110. }
  111. function bind(){
  112. //点击确认按钮
  113. $ok.click(doOk);
  114. //回车键触发确认按钮事件
  115. $(window).bind("keydown", function(e){
  116. if(e.keyCode == 13) {
  117. if($("#" + popId).length == 1){
  118. doOk();
  119. }
  120. }
  121. });
  122. //点击取消按钮
  123. $cancel.click(doCancel);
  124. //点击关闭按钮
  125. $clsBtn.click(doClose);
  126. }
  127. //确认按钮事件
  128. function doOk(){
  129. var $o = $(this);
  130. var v = $.trim($input.val());
  131. if ($input.is(":visible"))
  132. config.onOk(v);
  133. else
  134. config.onOk();
  135. $("#" + popId).remove();
  136. config.onClose(eventType.ok);
  137. }
  138. //取消按钮事件
  139. function doCancel(){
  140. var $o = $(this);
  141. config.onCancel();
  142. $("#" + popId).remove();
  143. config.onClose(eventType.cancel);
  144. }
  145. //关闭按钮事件
  146. function doClose(){
  147. $("#" + popId).remove();
  148. config.onClose(eventType.close);
  149. $(window).unbind("keydown");
  150. }
  151. //生成按钮组
  152. function creatBtnGroup(tp){
  153. var $bgp = $("<div>").addClass("btnGroup");
  154. $.each(btns, function(i, n){
  155. if( btnType[i] == (tp & btnType[i]) ){
  156. $bgp.append(n);
  157. }
  158. });
  159. return $bgp;
  160. }
  161. //重生popId,防止id重复
  162. function creatPopId(){
  163. var i = "pop_" + (new Date()).getTime()+parseInt(Math.random()*100000);//弹窗索引
  164. if($("#" + i).length > 0){
  165. return creatPopId();
  166. }else{
  167. return i;
  168. }
  169. }
  170. };
  171. //按钮类型
  172. window.wxc.xcConfirm.btnEnum = {
  173. ok: parseInt("0001",2), //确定按钮
  174. cancel: parseInt("0010",2), //取消按钮
  175. okcancel: parseInt("0011",2) //确定&&取消
  176. };
  177. //触发事件类型
  178. window.wxc.xcConfirm.eventEnum = {
  179. ok: 1,
  180. cancel: 2,
  181. close: 3
  182. };
  183. //弹窗类型
  184. window.wxc.xcConfirm.typeEnum = {
  185. info: "info",
  186. success: "success",
  187. error:"error",
  188. confirm: "confirm",
  189. warning: "warning",
  190. input: "input",
  191. custom: "custom"
  192. };
  193. })(jQuery);