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)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
随机推荐
- EF Code First、DbContext
EF Code First.DbContext 对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念一下泉涌而出,犹如当头一棒,的确有点不知所 ...
- 鸟哥的LINUX私房菜基础篇第三版 阅读笔记 二
Linux档案与目录管理 1.一些比较特殊的目录,需要用力的记下来 . 代表当前层目录 .. 代表上一层目录 - 代表前一个工作目录 (这个好屌!其他的 ...
- service structure flowchart [mobile to server via HTTP RESTful API]
Modern flowchart for mobile, server, and etc.. communication This has something to do with these sou ...
- MFC控件(9):network address control
这个控件的名字倒是取的不错,一看就知道是让你输入IP地址或host name的. 不过一打开看到那控件的样子就完全是个Edit control.不过该控件对应的类也确实是继承自类CEdit. 先拖个控 ...
- 从零开始学C++之虚继承和虚函数对C++对象内存模型造成的影响
首先重新回顾一下关于类/对象大小的计算原则: 类大小计算遵循结构体对齐原则 第一个数据成员放在offset为0的位置 其它成员对齐至min(sizeof(member),#pragma pack(n) ...
- Jquery控制点击时一、二级菜单自由隐藏与出现
一.基本HTML和CSS HTML中产生一.二级导航的代码: {loop $nav $key $value} <div class=”u_con”> <span class=” xg ...
- C#异步的世界【上】
新进阶的程序员可能对async.await用得比较多,却对之前的异步了解甚少.本人就是此类,因此打算回顾学习下异步的进化史. 本文主要是回顾async异步模式之前的异步,下篇文章再来重点分析async ...
- [转载]关于shell脚本的基本语法
关于shell脚本的基本语法 整理于:2014-03-31,何俭飞,mymladdr@sina.com 一.执行 1.shell脚本如果要被执行,一般地必须要有执行权限"x"(除了 ...
- canvas 弹幕效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- InnoDB的约束机制
数据完整性 关系型数据库系统和文件系统的一个不同点是,关系数据库本身能保证存储数据的完整性,不需要应用程序的控制,而文件系统一般需要在程序端进行控制.几乎所有的关系型数据库都提供了约束(constra ...