Javascript封装弹出框控件
1、首先先定义好弹出框的HTML结构
- <div class="g-dialog-contianer">
- <div class="dialog-window">
- <div class="dialog-header waiting"></div>
- <div class="dialog-container">你是否要清空购物车?</div>
- <div class="dialog-footer">
- <button class="green">按钮1</button>
- <button class="red">按钮2</button>
- </div>
- </div>
- </div>
2、编写好结构之后,编写CSS样式
- .g-dialog-contianer{
- position: fixed;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- background-color: rgba(0,0,0,0.6);
- display: -webkit-box;
- -webkit-box-pack:center;
- -webkit-box-align:center;
- }
- .g-dialog-contianer .dialog-window{
- padding: 1em;
- border-radius: 10px;
- background-color: rgba(0,0,0,0.8);
- }
- .g-dialog-contianer .dialog-window .dialog-header{
- width: 50px;
- height: 50px;
- margin: 0 auto;
- }
- .g-dialog-contianer .dialog-window .dialog-header.waiting{
- background: url("wait.png")no-repeat 0 0;
- background-size: 50px;
- }
- .g-dialog-contianer .dialog-window .dialog-header.warning{
- background: url("warning.png")no-repeat 0 0;
- background-size: 50px;
- }
- .g-dialog-contianer .dialog-window .dialog-header.success{
- background: url("success.png")no-repeat 0 0;
- background-size: 50px;
- }
- .g-dialog-contianer .dialog-window .dialog-container{
- padding: 1em 1em 0;
- color: #fff;
- line-height: 180%;
- text-align: center;
- }
- .g-dialog-contianer .dialog-window .dialog-footer{
- padding: 1em 1em 0;
- display: -webkit-flex;
- -webkit-justify-content:center;
- -webkit-align-items:center;
- }
- .g-dialog-contianer .dialog-window .dialog-footer button{
- -webkit-appearance:normal;
- background-color: rgba(255,255,255,0.8);
- padding: 0.8em 1.8em;
- border: none;
- color: #eee;
- border-radius: 5px;
- margin: 0 1.3em;
- text-shadow: #666 1px 1px 0;
- }
- .g-dialog-contianer .dialog-window .dialog-footer button.green{
- background-color: rgba(2,202,200,0.8);
- }
- .g-dialog-contianer .dialog-window .dialog-footer button.red{
- background-color: rgba(251,23,50,0.8);
- }
4、编写js代码
- //定义函数用于构造,来传递参数
- var Dialog = function(config) {
- //默认配置参数
- this.config = {
- //对话框宽高
- width: "auto",
- height: "auto",
- //对话框提示信息
- message: null,
- //对话框类型
- type: "waiting",
- //按钮配置
- buttons: null,
- //对话框保持时间3秒
- delay: null,
- //对话框遮罩层透明度
- maskOpcity: 0.8
- };
- //如果用户输入参数,将参数扩展
- if (config && $.isPlainObject(config)) {
- $.extend(this.config, config);
- }
- //给函数定义变量,并和config对象一起传入原型
- this.body = $("body");
- this.mask = $("<div class='g-dialog-contianer'>");
- this.win = $('<div class="dialog-window">');
- this.winHeader = $('<div class="dialog-header"></div>');
- this.winContent = $('<div class="dialog-container">');
- this.winFooter = $('<div class="dialog-footer">');
- };
- //原型中定义函数
- Dialog.prototype = {
- creat: function() {
- //1.this指的就是该原型对象
- //2.(this.)表示原型对象调用函数中的变量
- var _this_ = this,
- config = this.config,
- body = this.body,
- mask = this.mask,
- win = this.win,
- winHeader = this.winHeader,
- winContent = this.winContent,
- winFooter = this.winFooter;
- //如果用户没输入参数,默认弹出等待框,否则用扩展值
- win.append(winHeader.addClass(config.type));
- //如果用户输入massage,显示到弹框中
- if (config.message) {
- win.append(winContent.html(config.message));
- }
- //如果用户输入按钮组
- if (config.buttons) {
- this.creatButton(winFooter, config.buttons);
- win.append(winFooter);
- }
- //如果用户自定义弹出框的宽高
- if (config.width != 'auto') {
- win.width(config.width);
- }
- if (config.height != 'auto') {
- win.height(config.height);
- }
- //默认透明度为0.8
- var opct = config.maskOpcity;
- mask.css("backgroundColor", "rgba(0,0,0," + opct + ")");
- //如果用户输入弹框保持时间
- if (config.delay && config.delay !== 0) {
- window.setTimeout(function() {
- //调用原型中的close()方法
- _this_.close();
- }, config.delay);
- }
- //渲染html
- mask.append(win);
- body.append(mask);
- },
- //关闭弹出框
- close: function() {
- this.mask.remove();
- },
- //创建按钮组
- creatButton: function(footer, buttons) {
- var _this_ = this;
- //遍历出数组
- $(buttons).each(function(index, element) {
- var type = element.type ? " class=" + element.type : "";
- var text = element.text ? element.text : "button" + index;
- var callback = element.callback ? element.callback : null;
- var singleButton = $("<button" + type + ">" + text + "</button>");
- //如果有回调函数,按钮绑定回调函数
- if (callback) {
- singleButton.on('click', function() {
- callback();
- _this_.close();
- });
- }
- //否则默认为关闭弹出框
- else {
- singleButton.on('click', function() {
- _this_.close();
- });
- }
- footer.append(singleButton);
- });
- }
- };
- function startDialog1() {
- var dialog = new Dialog();
- dialog.creat();
- }
- function startDialog2() {
- var dialog = new Dialog({
- width: "auto",
- height: "auto",
- type: "warning",
- delay: 2000,
- });
- dialog.creat();
- }
- function startDialog3() {
- var dialog = new Dialog({
- width: "auto",
- height: "auto",
- type: "success",
- buttons: [{
- type: "green",
- text: "确定",
- }, {
- type: "red",
- text: "取消"
- }]
- });
- dialog.creat();
- }
- function startDialog4() {
- var dialog = new Dialog({
- width: "auto",
- height: "auto",
- type: "warning",
- buttons: [{
- type: "green",
- text: "确定",
- callback: function() {
- }
- }]
- });
- dialog.creat();
- }
- function startDialog5() {
- var dialog = new Dialog({
- width: "auto",
- height: "auto",
- buttons: [{
- type: "green",
- text: "确定",
- callback: function() {
- }
- }, {
- type: "green",
- text: "确定",
- callback: function() {
- }
- }, {
- type: "green",
- text: "确定",
- callback: function() {
- }
- }]
- });
- dialog.creat();
- }
- function startDialog6() {
- var dialog = new Dialog({
- width: "auto",
- height: "auto",
- message: "你是否要清空购物车?",
- type: "warning",
- buttons: [{
- type: "green",
- text: "确定",
- callback: function() {
- window.open('http://baidu.com');
- }
- }, {
- type: "red",
- text: "取消"
- }],
- maskOpcity: 0.6
- });
- dialog.creat();
- }
Javascript封装弹出框控件的更多相关文章
- EasyMvc入门教程-高级控件说明(18)弹出框控件
前面两节介绍了信息框与对话框,实际开发中如果我们遇到更复杂的要求,比如要求在弹出框里显示另外的网址,如下所示: 实现代码如下: @Html.Q().Popup().Text("我可以嵌套网页 ...
- angular 指令封装弹出框效果
就直接用bs的警告框啦~,Duang~ 功能 可以设置message和type,type就是bs内置的几种颜色 默认提示3秒框自动关闭,或者点击x号关闭 代码 模板 <div class=&qu ...
- JQuery+CSS3实现封装弹出登录框效果
原文:JQuery+CSS3实现封装弹出登录框效果 上次发了一篇使用Javascript来实现弹出层的效果,这次刚好用了JQuery来实现,所以顺便记录一下: 因为这次使用了Bootstrap来做一个 ...
- MFC编程入门之二十五(常用控件:组合框控件ComboBox)
上一节讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常见,例如,在Windows系统的控制面板上设置语言或位置时,有很多选项,用来进行选择的控件就是组合框 ...
- MFC编程入门之二十四(常用控件:列表框控件ListBox)
前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
- 在DWZ框架中整合kindeditor复文本框控件
今天上午在DWZ框架中整合kindeditor复文本框控件,发现上传图片是老是提示 “上传中,请稍候...”,上网查看别人说可能是文件路径问题,在想以前在其他项目中用这个控件一直没问题,到这里怎么会出 ...
- 【转】VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)
原文网址:http://www.jizhuomi.com/software/189.html 上一节鸡啄米讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常 ...
- VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)
上一节鸡啄米讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常见,例如,在Windows系统的控制面板上设置语言或位置时,有很多选项,用来进行选择的控件就是 ...
- VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)
前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
随机推荐
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...
- PyQt设计流程
Qt designer 设计流程: 创建一个 PyQt4 的步骤,通常的方法是用 QtDesigner 工具创建 GUI 界面.可以在窗口 上添加部件,并可以对部件进行一些属性配置.一般的过程如下 ...
- c# 在datagridview中添加comboboxcolumn 绑定数据库读取显示数据
datagridview中的comboboxcolumn 从绑定的数据库中读取显示时,只需要注意一点,就是sql语句加个 CStr() 字符串转换函数即可,如下: SELECT CStr(XXX) a ...
- 安全验证之使用摘要认证(digest authentication)
安全验证之使用摘要认证(digest authentication) 在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看 ...
- Static 单例模式
Static 方法的问题 今天在看EhCache源码的时候,发现有一个这样的方法 这个是个典型的单例模式的工具类,但我所知道的单例模式的写法不是这样的,虽然<effect java>中 ...
- Qt中的串口编程之三
QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...
- 代码创建 WPF 旋转动画
一.WPF窗体上有一个名为rectangle2的矩形 对应的XAML如下: <Window x:Class="WpfVideo.Window1" xmlns= ...
- 控制 Memory 和 CPU 资源的使用
Resource Governor的出现,解决了在一台SQL Server实例上,管理多用户工作负载和资源隔离的需求,它允许管理员限制系统处理Requsts时所耗费的CPU 和 Memory资源的数量 ...
- MVC源码解析 - 目录
尽管MVC6和MVC4,5已经有很大不同, 但是, 作为一个普通开发人员, 还真没有资格去选择使用哪个版本. So, 尽管已经是old的版本, 还是再次花点时间去温故知新. 我记得在15年初的时候, ...
- Mybatis学习笔记(一) 之框架原理
原生态JDBC编程中问题总结 1.单独使用jdbc连接数据库 maven依赖包: <!-- mysql --> <dependency> <groupId>mysq ...