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. Twitter Storm:单机环境的安装与配置

    Twitter Storm:单机环境的安装与配置 好久没写博客了,这一段时间一直被导师push着做毕业设计.由于目前的方向偏向于图像识别检索,毕设打算做一个基于分布式计算平台的图像检索系统,查阅相关资 ...

  2. linux下php-5.4.8.tar.gz编译安装全攻略

    首先安装基础依赖组建,注:这些依赖组建也是LINUX+PHP+MYSQL+APACHE+NGINX+MEMCACHED时必要的系统组件  LANG=C yum -y install gcc gcc-c ...

  3. [每日一题] OCP1z0-047 :2013-07-15 drop column

    如下实验: gyj@OCM> Create table emp( 2    Empno     number(4)    not null, 3    First_name  varchar2( ...

  4. shell脚本作为保证PHP脚本不挂掉的守护进程实例

    前几天开始跑一份数据名单,名单需要提供用户名.是否有手机号.是否有邮箱,用户名单我轻易的获取到了,但是,用户名单有2000w之多,并且去检测用户是否有手机号.是否有邮箱必须得通过一个对外开放的安全接口 ...

  5. iOS 测试驱动开发

    测试驱动开发是敏捷开发的一部分,它一般有“red-green- refactor”步骤 iOS测试驱动开发的工具 一. OCUnit 是Xcode自带的测试工具 其使用步骤分为 1 建立测试的Targ ...

  6. mac nodejs安装

    很久没有配置开发环境了,刚换了新电脑,正好借机会重新配置一下node相关的开发环境 安装 nvm :Node Version Manager 由于nodejs版本更新迭代较快,而不同版本间的差异又很大 ...

  7. 数据结构之Binary Search Tree (Java)

    二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...

  8. java-并发解决方案

    并发产生数据不一致的原因:1.程序共享对象:2.多线程.3.基于1和2,取出来的数据可能不是最新的. 解决方案:只要是原子性操作,就不会出现问题.原子性操作,代表cpu会一直执行这个操作,知道结束. ...

  9. 基于C++的类编程总结

    1. 类中public, protected, private这三个属性的区别: public意味着所有事物都能查询具有该属性的食物.(也即所有人可以在我不知情的情况下,查看我账户里还有多少钱). p ...

  10. bootstrap IE8 相互兼容

    针对 IE8 仍然需要额外引入 Respond.js 文件(由于仍然利用了浏览器对媒体查询(media query)的支持,因此还需要做处理) <html lang="zh-cn&qu ...