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)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
随机推荐
- Myeclipse新建 配置Hibernate
一.新建一个JAVA项目 二.选中新建的项目单击右键[Add Hibernate Capab-] 三.添加MyEclipse Hiberate Libaries(Hibernate 3.2) 单击[B ...
- PHP:执行模型和内存模型
PHP:执行模型和内存模型 背景 对于任何一种语言,了解其执行模型和内存模型都是有意义的,本文中的内容不见得正确,请多批评. 执行模型 每个请求都是一个独立的PHP进程,两个请求之间会完全隔离,会话和 ...
- Castle Windsor 项目中快速使用
Castle Windsor 项目中快速使用 新建项目如下: 一个模型类,一个接口,一个实现方法.我的目的很明确就是在UI层通过Castle 调用数据访问层的方法. 添加项目引用 CastleDemo ...
- 重构MVC多条件分页解决方案
重构MVC多条件+分页解决方案 为支持MVC的验证,无刷新查询,EF,以及让代码可读性更强一点,所以就重构了下原来的解决方案. 这里就简单讲下使用方法吧: Model: 继承PagerBase: S ...
- C Socket初探
C Socket初探 前段时间写了个C# Socket初探,这次再写个C语言的Socket博文,运行效果如下: 实现步骤: 1. Server端 #include <stdio.h> // ...
- C#基础知识回顾--线程传参
C#基础知识回顾--线程传参 在不传递参数情况下,一般大家都使用ThreadStart代理来连接执行函数,ThreadStart委托接收的函数不能有参数, 也不能有返回值.如果希望传递参数给执行函数, ...
- MFC 数据库编程 增删改查的一个例子
1.先看下效果图: 主界面: 一个最简单的1对多的表,一张Article(文章)表,一张Category(类别)表.有添加,修改,删除文章按钮.点击类别编辑按钮就会跳到第二个对话框.点击数据库配置就会 ...
- 三角形(Triangle)
三角形(Triangle) 问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8 ...
- Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
我们一般下载的应用在第一次启动应用的时候都会给我创建一个桌面快捷方式,然后我在网上找了些资料整理下了,写了一个快捷方式的工具类,这样我们以后要创建快捷方式的时候直接拷贝这个类,里面提供了一些静态方法, ...
- Android获取SD卡中选中图片的路径(URL)
最近在做一个图片上传的功能,需要提供上传图片在SD卡中的路径,在网上看了些例子,改改调试成功,代码很简单.其布局文件如下: [html] view plain copy <?xml ver ...