一、参数设置

$.beamDialog(options);
var defaults = {
title:'标题',
content:'内容',
showCloseButton:true,
//显示关闭按钮
otherButtons:[],
//其他按钮文本,样式默认,["确定","取消"]
otherButtonStyles:[],
//其他按钮的样式,['btn-primary','btn-primary'],bootstrap按钮样式
bsModalOption:{},
//默认的bootstrap模态对话框参数
dialogShow:function(){},
//对话框即将显示事件
dialogShown:function(){},
//对话框已经显示事件
dialogHide:function(){},
//对话框即将关闭
dialogHidden:function(){},
//对话框已经关闭事件
clickButton:function(sender,modal,index){}
}

二、完整例子代码

$.beamDialog({
title:'系统提示',
content:'确认删除本条记录?',
showCloseButton:true,
otherButtons:["确定","取消"],
otherButtonStyles:['btn-primary','btn-primary'],
bsModalOption:{keyboard: true},
dialogShow:function(){
alert('即将显示对话框');
},
dialogShown:function(){
alert('显示对话框');
},
dialogHide:function(){
alert('即将关闭对话框');
},
dialogHidden:function(){
alert('关闭对话框');
},
clickButton:function(sender,modal,index){
alert('选中第'+index+'个按钮:'+sender.html());
$(this).closeDialog(modal);
}
});

三、简单调用代码例子

obj.event function(){
$.beamDialog({
title:'系统提示',
content:'确认删除本条记录?'
});
}

封装代码:

(function($) {
$.fn.beamDialog = function(options) {
var defaults = {
title: '标题',
content: '<p>内容</p>',
showCloseButton: true,
otherButtons: [],
otherButtonStyles: [],
bootstrapModalOption: {},
dialogShow: function() {},
dialogShown: function() {},
dialogHide: function() {},
dialogHidden: function() {},
clickButton: function(sender, modal, index) {}
};
options = $.extend(defaults, options);
var modalID = ''; //生成一个唯一的ID
function random(a, b) {
return Math.random() > 0.5 ? -1 : 1;
} function getModalID() {
return "beamDialog-" + ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'Q', 'q', 'W', 'w', 'E', 'e', 'R', 'r', 'T', 't', 'Y', 'y', 'U', 'u', 'I', 'i', 'O', 'o', 'P', 'p', 'A', 'a', 'S', 's', 'D', 'd', 'F', 'f', 'G', 'g', 'H', 'h', 'J', 'j', 'K', 'k', 'L', 'l', 'Z', 'z', 'X', 'x', 'C', 'c', 'V', 'v', 'B', 'b', 'N', 'n', 'M', 'm'].sort(random).join('').substring(5, 20);
} $.fn.extend({
closeDialog: function(modal) {
var modalObj = modal;
modalObj.modal('hide');
}
}); return this.each(function() {
var obj = $(this);
modalID = getModalID();
var tmpHtml = '<div class="modal fade" id="{ID}" role="dialog" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button><h6 class="modal-title">{title}</h6></div><div class="modal-body">{body}</div><div class="modal-footer">{button}</div></div></div></div>';
var buttonHtml = '<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>';
if (!options.showCloseButton && options.otherButtons.length > 0) {
buttonHtml = '';
}
//生成按钮
var btnClass = 'cls-' + modalID;
for (var i = 0; i < options.otherButtons.length; i++) {
buttonHtml += '<button buttonIndex="' + i + '" class="' + btnClass + ' btn ' + options.otherButtonStyles[i] + '">' + options.otherButtons[i] + '</button>';
}
//替换模板标记
tmpHtml = tmpHtml.replace(/{ID}/g, modalID).replace(/{title}/g, options.title).replace(/{body}/g, options.content).replace(/{button}/g, buttonHtml);
obj.append(tmpHtml); var modalObj = $('#' + modalID);
//绑定按钮事件,不包括关闭按钮
$('.' + btnClass).click(function() {
var index = $(this).attr('buttonIndex');
options.clickButton($(this), modalObj, index);
});
//绑定本身的事件
modalObj.on('show.bs.modal', function() {
options.dialogShow();
});
modalObj.on('shown.bs.modal', function() {
options.dialogShown();
});
modalObj.on('hide.bs.modal', function() {
options.dialogHide();
});
modalObj.on('hidden.bs.modal', function() {
options.dialogHidden();
modalObj.remove();
});
modalObj.modal(options.bootstrapModalOption);
}); }; $.extend({
beamDialog: function(options) {
$("body").beamDialog(options);
}
}); })(jQuery);

基于bootstrap模态框的二次封装的更多相关文章

  1. 基于bootstrap模态框的alert弹窗

    完成的效果如下: html代码: <!-- 弹出框 --> <div class="modal fade" id="alert_like" t ...

  2. 基于bootstrap模态框的日期选择器

    近来由于工作需求,以bootstrap模态框+DIV+CSS+JS做了一个适用于移动端的日期选择器,能够满足多样的需求,目前处于第一个版本,后续可能会继续更新.废话不多说,直接进入制作过程. 首先,需 ...

  3. 基于bootstrap table配置的二次封装

    准备 jQuery js css 引用完毕 开始 如果对bootstrap table 的方法与事件不熟悉: Bootstrap table方法,Bootstrap table事件 <table ...

  4. 基于bootstrap模态框、fakeLoader实现全局遮罩层

    一.fakeLoader.js介绍 fakeLoader.js 是轻量级的 jQuery 插件,帮助你创建动态的全屏加载掩饰微调效果,模拟页面预加载的效果. 插件下载地址:https://github ...

  5. 第二百四十三节,Bootstrap模态框插件

    Bootstrap模态框插件 学习要点: 1.基本使用 2.用法说明 本节课我们主要学习一下 Bootstrap 中的模态框插件,这是一款交互式网站非常常见的 弹窗功能插件. 一.基本使用 使用模态框 ...

  6. bootstrap模态框modal使用remote第二次加载显示相同内容解决办法

    bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法 bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 ...

  7. JavaScript:bootstrap 模态框的简单应用

    最近用上了bootstrap这个强大的前端框架,有空来总结一下.这里记录下模态框的简单应用. 首先,要在页面中引入相应的js.css文件 <link href="css/bootstr ...

  8. js控制Bootstrap 模态框(Modal)插件

    js控制Bootstrap 模态框(Modal)插件 http://www.cnblogs.com/zzjeny/p/5564400.html

  9. Bootstrap模态框按钮

    1.触发模态框弹窗的代码 这里复制了一段Bootstrap模态框的代码 <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <but ...

随机推荐

  1. 【ORACLE】sqlplus使用记录

    1.设置输出长度 SEGMENT_NAME--------------------------- BYTES----------TZ01_LOGIN_DATA 20971520 TZ02_EP_GAT ...

  2. ife task0003学习笔记(二):JavaScript原型

    function aaa(){} aaa.prototype.bbb=function(){} var obj1=new aaa() var obj2=new aaa() obj1和obj2都有一个属 ...

  3. postgres formencode.api.Invalid

    错误提示: Invalid: expected an int in the IntCol 'geom', got <type 'str'> '010100000007EBFFFC3A611 ...

  4. C++程序设计基础(2)变量

    注:读<程序员面试笔记>笔记总结 1.知识点 (1)C++变量命名只能包含字母.数字.下划线,其中开头不能是数字:大小写敏感:习惯上变量用小写字母,常量.宏定义用大写字母. (2)变量的作 ...

  5. MySQL中设置同一张表中一个字段的值等于另一个字段的值

    今天遇到了一个需求,我在一张表中新增了一个字段,因为这张表以前已经有很多数据了,这样对于以前的数据来说,新增的这个字段的值也就是为该字段的默认值,现在需要将新增的这个字段添加上数据,数据来源为同表的另 ...

  6. MongoDB windows基础配置及集群搭建

    由于公司业务的发展MSSQL已处于瓶颈.因为没钱买牛逼服务器只能靠软件来实现最大优化了.原来的系统架构如下图:

  7. 进程和程序(Process and Program)

    原出处:http://oss.org.cn/kernel-book/ch04/4.1.htm ----------------------------------个人理解分割线------------ ...

  8. C#简单实现读取txt文本文件并分页存储到数组

    最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...

  9. CMDB认识和需求分析

    一.认识ITIL   ITIL即IT基础架构库(Information Technology Infrastructure Library,信息技术基础架构库)由英国政府部门CCTA(Central ...

  10. JS文本中间显示省略号

    众所周知,文本溢出显示省略号用CSS就可以: 单行文本: white-space: nowrap; text-overflow: ellipsis; overflow: hidden; display ...