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封装弹出框控件的更多相关文章

  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. Fortran使用隐形DO循环和reshape给一维和多维数组赋初值

    Fortran可以使用隐形DO循环和reshape给一维和多维数组赋初值. 下面以一维数组和二维数组为例,并给出程序结果: program main implicit none integer::i, ...

  2. PHP, Python Nginx works together!

    Nginx is so good at delivering requests to many others. Good! Now let's use the nginx upstream modul ...

  3. ADS的go to命令

    我们有时候在一个函数右键没有看到“go to”选项,这是因为没有Make,要先Make之后才能使用go to 命令

  4. android sqlite使用之模糊查询数据库数据的三种方式

    android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sqlite的模糊查询方式,直接上代码把: package com.e ...

  5. linux 下把Caps_Lock 映射成Ctrl

    我用的是debian 7,以前是gnome桌面,系统里就有改变键盘布局的设置.之前gnome 崩了一次,就换成了openbox ,稍微配置了一下也可以很好使用. 可以参考这篇文章 5分钟openbox ...

  6. 用Bottle开发web程序(二)

    返回码 在开发web程序时,除了一些服务器错误等,常常需要自定义返回码,以便告诉用户处理请求的结果或者状态.bottle支持自定义的返回码,可以通过以下几种方式进行实现. abort 在bottle中 ...

  7. Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  8. 01--css编码技巧--css揭秘

    一 尽量减少代码重复 1.按钮 #btn { padding: .3em .8em; border: 1px solid #446d88; background: #58a linear-gradie ...

  9. Maven之(七)pom.xml配置文件详解

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  10. vim实用技巧总结

    1. 单个文件内实用mark来帮助跳转以及一些宏操作.(注意:mark不能用于多文件之间的跳转)mch 设置宏,保存到ch标记(不是ch寄存器)'ch 跳转到标记的行首`ch 跳转到标记的字符处另:为 ...