1、首先先定义好弹出框的HTML结构

  1. <div class="g-dialog-contianer">
  2. <div class="dialog-window">
  3. <div class="dialog-header waiting"></div>
  4. <div class="dialog-container">你是否要清空购物车?</div>
  5. <div class="dialog-footer">
  6. <button class="green">按钮1</button>
  7. <button class="red">按钮2</button>
  8. </div>
  9. </div>
  10. </div>

2、编写好结构之后,编写CSS样式

  1. .g-dialog-contianer{
  2. position: fixed;
  3. left: 0;
  4. top: 0;
  5. width: 100%;
  6. height: 100%;
  7. background-color: rgba(0,0,0,0.6);
  8. display: -webkit-box;
  9. -webkit-box-pack:center;
  10. -webkit-box-align:center;
  11. }
  12. .g-dialog-contianer .dialog-window{
  13. padding: 1em;
  14. border-radius: 10px;
  15. background-color: rgba(0,0,0,0.8);
  16. }
  17. .g-dialog-contianer .dialog-window .dialog-header{
  18. width: 50px;
  19. height: 50px;
  20. margin: 0 auto;
  21. }
  22. .g-dialog-contianer .dialog-window .dialog-header.waiting{
  23. background: url("wait.png")no-repeat 0 0;
  24. background-size: 50px;
  25. }
  26. .g-dialog-contianer .dialog-window .dialog-header.warning{
  27. background: url("warning.png")no-repeat 0 0;
  28. background-size: 50px;
  29. }
  30. .g-dialog-contianer .dialog-window .dialog-header.success{
  31. background: url("success.png")no-repeat 0 0;
  32. background-size: 50px;
  33. }
  34. .g-dialog-contianer .dialog-window .dialog-container{
  35. padding: 1em 1em 0;
  36. color: #fff;
  37. line-height: 180%;
  38. text-align: center;
  39. }
  40. .g-dialog-contianer .dialog-window .dialog-footer{
  41. padding: 1em 1em 0;
  42. display: -webkit-flex;
  43. -webkit-justify-content:center;
  44. -webkit-align-items:center;
  45. }
  46. .g-dialog-contianer .dialog-window .dialog-footer button{
  47. -webkit-appearance:normal;
  48. background-color: rgba(255,255,255,0.8);
  49. padding: 0.8em 1.8em;
  50. border: none;
  51. color: #eee;
  52. border-radius: 5px;
  53. margin: 0 1.3em;
  54. text-shadow: #666 1px 1px 0;
  55. }
  56. .g-dialog-contianer .dialog-window .dialog-footer button.green{
  57. background-color: rgba(2,202,200,0.8);
  58. }
  59. .g-dialog-contianer .dialog-window .dialog-footer button.red{
  60. background-color: rgba(251,23,50,0.8);
  61. }

4、编写js代码

  1. //定义函数用于构造,来传递参数
  2. var Dialog = function(config) {
  3. //默认配置参数
  4. this.config = {
  5. //对话框宽高
  6. width: "auto",
  7. height: "auto",
  8. //对话框提示信息
  9. message: null,
  10. //对话框类型
  11. type: "waiting",
  12. //按钮配置
  13. buttons: null,
  14. //对话框保持时间3秒
  15. delay: null,
  16. //对话框遮罩层透明度
  17. maskOpcity: 0.8
  18. };
  19. //如果用户输入参数,将参数扩展
  20. if (config && $.isPlainObject(config)) {
  21. $.extend(this.config, config);
  22. }
  23. //给函数定义变量,并和config对象一起传入原型
  24. this.body = $("body");
  25. this.mask = $("<div class='g-dialog-contianer'>");
  26. this.win = $('<div class="dialog-window">');
  27. this.winHeader = $('<div class="dialog-header"></div>');
  28. this.winContent = $('<div class="dialog-container">');
  29. this.winFooter = $('<div class="dialog-footer">');
  30. };
  31.  
  32. //原型中定义函数
  33. Dialog.prototype = {
  34. creat: function() {
  35. //1.this指的就是该原型对象
  36. //2.(this.)表示原型对象调用函数中的变量
  37. var _this_ = this,
  38. config = this.config,
  39. body = this.body,
  40. mask = this.mask,
  41. win = this.win,
  42. winHeader = this.winHeader,
  43. winContent = this.winContent,
  44. winFooter = this.winFooter;
  45. //如果用户没输入参数,默认弹出等待框,否则用扩展值
  46. win.append(winHeader.addClass(config.type));
  47. //如果用户输入massage,显示到弹框中
  48. if (config.message) {
  49. win.append(winContent.html(config.message));
  50. }
  51. //如果用户输入按钮组
  52. if (config.buttons) {
  53. this.creatButton(winFooter, config.buttons);
  54. win.append(winFooter);
  55. }
  56. //如果用户自定义弹出框的宽高
  57. if (config.width != 'auto') {
  58. win.width(config.width);
  59. }
  60. if (config.height != 'auto') {
  61. win.height(config.height);
  62. }
  63. //默认透明度为0.8
  64. var opct = config.maskOpcity;
  65. mask.css("backgroundColor", "rgba(0,0,0," + opct + ")");
  66. //如果用户输入弹框保持时间
  67. if (config.delay && config.delay !== 0) {
  68. window.setTimeout(function() {
  69. //调用原型中的close()方法
  70. _this_.close();
  71. }, config.delay);
  72. }
  73. //渲染html
  74. mask.append(win);
  75. body.append(mask);
  76. },
  77.  
  78. //关闭弹出框
  79. close: function() {
  80. this.mask.remove();
  81. },
  82.  
  83. //创建按钮组
  84. creatButton: function(footer, buttons) {
  85. var _this_ = this;
  86. //遍历出数组
  87. $(buttons).each(function(index, element) {
  88. var type = element.type ? " class=" + element.type : "";
  89. var text = element.text ? element.text : "button" + index;
  90. var callback = element.callback ? element.callback : null;
  91.  
  92. var singleButton = $("<button" + type + ">" + text + "</button>");
  93. //如果有回调函数,按钮绑定回调函数
  94. if (callback) {
  95. singleButton.on('click', function() {
  96. callback();
  97. _this_.close();
  98. });
  99. }
  100. //否则默认为关闭弹出框
  101. else {
  102. singleButton.on('click', function() {
  103. _this_.close();
  104. });
  105. }
  106. footer.append(singleButton);
  107. });
  108. }
  109. };
  110.  
  111. function startDialog1() {
  112. var dialog = new Dialog();
  113. dialog.creat();
  114. }
  115.  
  116. function startDialog2() {
  117. var dialog = new Dialog({
  118. width: "auto",
  119. height: "auto",
  120. type: "warning",
  121. delay: 2000,
  122. });
  123. dialog.creat();
  124. }
  125.  
  126. function startDialog3() {
  127. var dialog = new Dialog({
  128. width: "auto",
  129. height: "auto",
  130. type: "success",
  131. buttons: [{
  132. type: "green",
  133. text: "确定",
  134. }, {
  135. type: "red",
  136. text: "取消"
  137. }]
  138. });
  139. dialog.creat();
  140. }
  141.  
  142. function startDialog4() {
  143. var dialog = new Dialog({
  144. width: "auto",
  145. height: "auto",
  146. type: "warning",
  147. buttons: [{
  148. type: "green",
  149. text: "确定",
  150. callback: function() {
  151.  
  152. }
  153. }]
  154. });
  155. dialog.creat();
  156. }
  157.  
  158. function startDialog5() {
  159. var dialog = new Dialog({
  160. width: "auto",
  161. height: "auto",
  162. buttons: [{
  163. type: "green",
  164. text: "确定",
  165. callback: function() {
  166.  
  167. }
  168. }, {
  169. type: "green",
  170. text: "确定",
  171. callback: function() {
  172.  
  173. }
  174. }, {
  175. type: "green",
  176. text: "确定",
  177. callback: function() {
  178.  
  179. }
  180. }]
  181. });
  182. dialog.creat();
  183. }
  184.  
  185. function startDialog6() {
  186. var dialog = new Dialog({
  187. width: "auto",
  188. height: "auto",
  189. message: "你是否要清空购物车?",
  190. type: "warning",
  191. buttons: [{
  192. type: "green",
  193. text: "确定",
  194. callback: function() {
  195. window.open('http://baidu.com');
  196. }
  197. }, {
  198. type: "red",
  199. text: "取消"
  200. }],
  201. maskOpcity: 0.6
  202. });
  203. dialog.creat();
  204. }

Javascript封装弹出框控件的更多相关文章

  1. EasyMvc入门教程-高级控件说明(18)弹出框控件

    前面两节介绍了信息框与对话框,实际开发中如果我们遇到更复杂的要求,比如要求在弹出框里显示另外的网址,如下所示: 实现代码如下: @Html.Q().Popup().Text("我可以嵌套网页 ...

  2. angular 指令封装弹出框效果

    就直接用bs的警告框啦~,Duang~ 功能 可以设置message和type,type就是bs内置的几种颜色 默认提示3秒框自动关闭,或者点击x号关闭 代码 模板 <div class=&qu ...

  3. JQuery+CSS3实现封装弹出登录框效果

    原文:JQuery+CSS3实现封装弹出登录框效果 上次发了一篇使用Javascript来实现弹出层的效果,这次刚好用了JQuery来实现,所以顺便记录一下: 因为这次使用了Bootstrap来做一个 ...

  4. MFC编程入门之二十五(常用控件:组合框控件ComboBox)

    上一节讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常见,例如,在Windows系统的控制面板上设置语言或位置时,有很多选项,用来进行选择的控件就是组合框 ...

  5. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  6. 在DWZ框架中整合kindeditor复文本框控件

    今天上午在DWZ框架中整合kindeditor复文本框控件,发现上传图片是老是提示 “上传中,请稍候...”,上网查看别人说可能是文件路径问题,在想以前在其他项目中用这个控件一直没问题,到这里怎么会出 ...

  7. 【转】VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)

    原文网址:http://www.jizhuomi.com/software/189.html 上一节鸡啄米讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常 ...

  8. VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)

    上一节鸡啄米讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常见,例如,在Windows系统的控制面板上设置语言或位置时,有很多选项,用来进行选择的控件就是 ...

  9. VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

随机推荐

  1. 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录

    利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...

  2. PyQt设计流程

    Qt designer 设计流程:  创建一个 PyQt4 的步骤,通常的方法是用 QtDesigner 工具创建 GUI 界面.可以在窗口  上添加部件,并可以对部件进行一些属性配置.一般的过程如下 ...

  3. c# 在datagridview中添加comboboxcolumn 绑定数据库读取显示数据

    datagridview中的comboboxcolumn 从绑定的数据库中读取显示时,只需要注意一点,就是sql语句加个 CStr() 字符串转换函数即可,如下: SELECT CStr(XXX) a ...

  4. 安全验证之使用摘要认证(digest authentication)

    安全验证之使用摘要认证(digest authentication) 在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看 ...

  5. Static 单例模式

    Static 方法的问题 今天在看EhCache源码的时候,发现有一个这样的方法   这个是个典型的单例模式的工具类,但我所知道的单例模式的写法不是这样的,虽然<effect java>中 ...

  6. Qt中的串口编程之三

    QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...

  7. 代码创建 WPF 旋转动画

    一.WPF窗体上有一个名为rectangle2的矩形 对应的XAML如下: <Window x:Class="WpfVideo.Window1"         xmlns= ...

  8. 控制 Memory 和 CPU 资源的使用

    Resource Governor的出现,解决了在一台SQL Server实例上,管理多用户工作负载和资源隔离的需求,它允许管理员限制系统处理Requsts时所耗费的CPU 和 Memory资源的数量 ...

  9. MVC源码解析 - 目录

    尽管MVC6和MVC4,5已经有很大不同, 但是, 作为一个普通开发人员, 还真没有资格去选择使用哪个版本. So, 尽管已经是old的版本, 还是再次花点时间去温故知新. 我记得在15年初的时候, ...

  10. Mybatis学习笔记(一) 之框架原理

    原生态JDBC编程中问题总结 1.单独使用jdbc连接数据库 maven依赖包: <!-- mysql --> <dependency> <groupId>mysq ...