基于bootstrap模态框的二次封装
一、参数设置
$.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">×</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模态框的二次封装的更多相关文章
- 基于bootstrap模态框的alert弹窗
完成的效果如下: html代码: <!-- 弹出框 --> <div class="modal fade" id="alert_like" t ...
- 基于bootstrap模态框的日期选择器
近来由于工作需求,以bootstrap模态框+DIV+CSS+JS做了一个适用于移动端的日期选择器,能够满足多样的需求,目前处于第一个版本,后续可能会继续更新.废话不多说,直接进入制作过程. 首先,需 ...
- 基于bootstrap table配置的二次封装
准备 jQuery js css 引用完毕 开始 如果对bootstrap table 的方法与事件不熟悉: Bootstrap table方法,Bootstrap table事件 <table ...
- 基于bootstrap模态框、fakeLoader实现全局遮罩层
一.fakeLoader.js介绍 fakeLoader.js 是轻量级的 jQuery 插件,帮助你创建动态的全屏加载掩饰微调效果,模拟页面预加载的效果. 插件下载地址:https://github ...
- 第二百四十三节,Bootstrap模态框插件
Bootstrap模态框插件 学习要点: 1.基本使用 2.用法说明 本节课我们主要学习一下 Bootstrap 中的模态框插件,这是一款交互式网站非常常见的 弹窗功能插件. 一.基本使用 使用模态框 ...
- bootstrap模态框modal使用remote第二次加载显示相同内容解决办法
bootstrap模态框modal使用remote动态加载内容,第二次加载显示相同内容解决办法 bootstrap的modal中,使用remote可以动态加载页面到modal-body中,并弹窗显示 ...
- JavaScript:bootstrap 模态框的简单应用
最近用上了bootstrap这个强大的前端框架,有空来总结一下.这里记录下模态框的简单应用. 首先,要在页面中引入相应的js.css文件 <link href="css/bootstr ...
- js控制Bootstrap 模态框(Modal)插件
js控制Bootstrap 模态框(Modal)插件 http://www.cnblogs.com/zzjeny/p/5564400.html
- Bootstrap模态框按钮
1.触发模态框弹窗的代码 这里复制了一段Bootstrap模态框的代码 <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <but ...
随机推荐
- [转]一种可以避免数据迁移的分库分表scale-out扩容方式
原文地址:http://jm-blog.aliapp.com/?p=590 目前绝大多数应用采取的两种分库分表规则 mod方式 dayofweek系列日期方式(所有星期1的数据在一个库/表,或所有?月 ...
- TOJ 4008 The Leaf Eaters(容斥定理)
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...
- BNU 20950 ——沉重的货物 —————— · 最短路、最短边最大化」
沉重的货物 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 吴恩达《Machine Learning Yearning》总结(1-10章)
1.为什么选择机器学习策略 案例:建立猫咪图像识别app 系统的优化可以有很多的方向: (1)获取更多的数据集,即更多的图片: (2)收集更多多样数据,如处于不常见的位置的猫的图,颜色奇异的猫的照片等 ...
- nginx禁止对写操作timeout时retry
1) nginx禁止对写操作timeout时retry 以前遇到的一个case,业务那边说一笔请求从nginx端发送给后端tomcat了2次(落在两个不同的tomcat节点上).后来发现是nginx发 ...
- [转] .net core Session , Working with a distributed cache
本文转自:https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed By Steve Smith+ Di ...
- Java 访问权限控制- protected 关键字
protected 关键字的真正内涵 文章来源:http://blog.csdn.net/justloveyou_/article/details/61672133 很多介绍Java语言的书籍(包括& ...
- 课堂笔记&总结与遇错纠错篇
一.课堂笔记 二.个人总结 在学习和工作JDK是必不可少的程序员必备工具,遇到问题可以在帮助文档寻找答案! 接受能力不足,老师讲的知识点过去了,我经常还在想上一个知识点.希望老师有时候重点可以讲慢点哈 ...
- Windows10 iis10 arr webfarm
win10安装arr提示安装失败,但是安装说明中提升iis7及以上版本,iis10却安装失败,坑爹!安装方法: 1.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSe ...