对bootstrap modal的简单扩展封装

对bootstrap modal的简单扩展封装

参考自:http://www.muzilei.com/archives/677   注:原文不支持bootstrap新版本,并且居中等存在问题

  此段时间一直在学习bootstrap,主要是用于做后台,一直习惯用easyui,ui,jgrid前端框架,以至于并不习惯bootstrap的风格。近来考虑到easyui性能不太好,就用了bootstrap,先说下我所了解的bootstrap. 1.外国的框架用于显示中文看着总是不妥. 2.默认的样式觉得有些松散;比如表格,必须加上table-condensed类才显得紧凑一些。

  进入正题,在使用bootstrap 模态对话框时需要在页面写对话框html,如果一个页面有许多地方需要对话框,那意味着需要写多个,感觉很麻烦,平时不太习惯bootstrap 模态对话框这种方式,所以做了个简单封装及扩展,增加了自定义标题,宽度和高度,并根据宽高居中显示

  调用代码

1             <a class="mzDialog" href="#" data-remote="/AccountingPeriod/Init" data-mtitle="哈哈" data-id="m1" data-width="600" data-height="500" data-backdrop=false data-okevent="ok()" data-openevent="open()">弹窗demo</a>

 

        $(".mzDialog").wwDialog();

  目前只支持以data-属性调用,不支持以js方式调用,欢迎大家扩展一下哦

  参数介绍

 1 id:"modal",//弹窗id
 2 title:"dialog",//弹窗标题
 3 width:"600",//弹窗宽度,暂时不支持%
 4 height:"500",//弹窗高度,不支持%
 5 backdrop:true,//是否显示遮障,和原生bootstrap 模态框一样
 6 keyboard:true,//是否开启esc键退出,和原生bootstrap 模态框一样
 7 remote:"",//加载远程url,和原生bootstrap 模态框一样
 8 openEvent:null,//弹窗打开后回调函数
 9 closeEvent:null,//弹窗关闭后回调函数
10 okEvent:null//单击确定按钮回调函数

  下面是源代码:

  1 /*------------------------------------------------------
  2      *封装的dialog插件,基于bootstrap模态窗口的简单扩展
  3      *作者:muzilei
  4      *修改人:lyw 原插件对bootstrap3.3.0不支持
  5      *email:[email protected]
  6 -------------------------------------------------------*/
  7 /*----------------------------------------------------------------------------------------------------
  8 1、bootstrap-wwDialog 插件暂时只有2个按钮,取消和确定,暂不支持自定义按钮,自己可以修改源代码添加此功能。
  9
 10 2、只能使用html data-*方式定义,不支持js初始化时配置参数,自己可以修改源码扩展此功能。
 11
 12 3、宽度和高度建议不要使用百分比
 13
 14 4、注意这里回调函数必需是字符串格式,如okEvent:”ok()” 这里ok函数式自己定义的函数,切记要带();
 15 ------------------------------------------------------------------------------------------------------*/
 16 (function ($) {
 17     $.fn.wwDialog = function () {
 18         var defaults = {
 19             id: "modal",//弹窗id
 20             title: "dialog",//弹窗标题
 21             width: "600",//弹窗宽度,暂时不支持%
 22             height: "500",//弹窗高度,不支持%
 23             backdrop: false,//是否显示遮障,和原生bootstrap 模态框一样
 24             keyboard: true,//是否开启esc键退出,和原生bootstrap 模态框一样
 25             remote: "",//加载远程url,和原生bootstrap 模态框一样
 26             openEvent: null,//弹窗打开后回调函数
 27             closeEvent: null,//弹窗关闭后回调函数
 28             okEvent: null//单击确定按钮回调函数
 29         };
 30
 31         //动态创建窗口
 32         var creatDialog = {
 33             init: function (opts) {
 34                 var _self = this;
 35
 36                 //动态插入窗口
 37                 var d = _self.dHtml(opts);
 38                 $("body").append(d);
 39
 40                 var modal = $("#" + opts.id);
 41
 42                 //初始化窗口
 43                 //modal.modal(opts);
 44                 modal.modal({
 45                     backdrop: false,
 46                     keyboard: opts.keyboard
 47                 });
 48                 $(".modal-body").load(opts.remote);
 49
 50                 //窗口大小位置
 51                 var h = modal.height() - modal.find(".modal-header").outerHeight() - modal.find(".modal-footer").outerHeight() - 5;
 52                 //modal.css({ ‘margin-left‘: opts.width / 2 * -1, ‘margin-top‘: opts.height / 2 * -1, ‘top‘: ‘50%‘ }).find(".modal-body").innerHeight(h);
 53                 modal.css({
 54                     position: "absolute",
 55                     left: ($(window).width() - opts.width) / 2,
 56                     top: ($(document).height() - opts.height) / 2
 57                 });
 58                 $(".modal-body").css({
 59                     height: opts.height - 115
 60                 });
 61                 modal
 62                 //显示窗口
 63                 .modal(‘show‘)
 64                 //隐藏窗口后删除窗口html
 65                 .on(‘hidden‘, function () {
 66                     modal.remove();
 67                     $(".modal-backdrop").remove();
 68                     if (opts.closeEvent) {
 69                         eval(opts.closeEvent);
 70                     }
 71                 })
 72                 //窗口显示后
 73                 .on(‘shown‘, function () {
 74
 75                     if (opts.openEvent) {
 76                         eval(opts.openEvent);
 77                     }
 78
 79
 80                 });
 81                 //绑定按钮事件
 82                 $(".ok").click(function () {
 83                     if (opts.okEvent) {
 84                         var ret = eval(opts.okEvent);
 85                         if (ret) {
 86                             modal.modal(‘hide‘);
 87                         }
 88                     }
 89                 });
 90             },
 91             dHtml: function (o) {
 92                 return ‘<div id="‘ + o.id + ‘" class="modal fade" role="dialog" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" ><div style=" background-color: #000;bottom: 0;left: 0;position: fixed;right: 0;top: 0;transition: opacity 0.15s linear 0s;opacity: 0.5;"></div><div class="modal-dialog" style="display:table-cell"><div class="modal-content" style="width:‘ + o.width + ‘px;height:‘ + o.height + ‘px;"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" ><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><h4 id="myModalLabel" class="modal-title">‘ + o.title + ‘</h4></div><div class="modal-body" ><p>正在加载...</p></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">取消</button><button class="btn btn-primary ok">确定</button></div></div></div></div>‘;
 93             }
 94         };
 95
 96         return this.each(function () {
 97             $(this).click(function () {
 98                 var opts = $.extend({}, defaults, {
 99                     id: $(this).attr("data-id"),
100                     title: $(this).attr("data-mtitle"),
101                     width: $(this).attr("data-width"),
102                     height: $(this).attr("data-height"),
103                     backdrop: $(this).attr("data-backdrop"),
104                     keyboard: $(this).attr("data-keyboard"),
105                     remote: $(this).attr("data-remote"),
106                     openEvent: $(this).attr("data-openEvent"),
107                     closeEvent: $(this).attr("data-closeEvent"),
108                     okEvent: $(this).attr("data-okEvent")
109                 });
110                 $(".modal").remove();
111                 creatDialog.init(opts);
112             });
113         });
114
115     };
116
117 })(jQuery);
时间: 2024-08-04 01:37:42

对bootstrap modal的简单扩展封装的相关文章

bootstrap modal 模态框拖拽扩展

主管要求bootstrap modal 带有拖拽移动效果.代码如下: JS 1 // bootstrap 模态框窗口 移动扩展, 在bootstrap 初始化后 调用 2 var btModalMoveEx = function () { 3 function moveEx($this) { 4 var $head = $this.find(".modal-header"), $dialog = $this.find(".modal-dialog"); 5 var

ServiceStack.OrmLite简单扩展

ServiceStack.OrmLite框架将所有的方法都扩展到了一个IDbConnection对象上,使用起来不是 很方便,在此对该框架做一个简单的封装. OrmLiteExecFilterExt类 using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Threading.Tasks; namespace ServiceStack.OrmLite.Ex

对bootstrap中confirm alert进行封装

HTML: <!-- system modal start --> <div id="ycf-alert" class="modal"> <div class="modal-dialog modal-sm"> <div class="modal-content"> <div class="modal-header"> <button type

简单扩展让beetl HTML标签支持父子嵌套

默认情况下,Beetl的html标签并不支持父子嵌套,就像类似jsp标签那样,父标签需要知道子标签的信息,子标签也需要知道父标签信息.但是beetl只需要简单扩展,就能完成嵌套标签支持. 首先看一个最终的使用效果,实现俩个html标签table.tag,tr.tag.可以在页面上这么用: <#table data ="${userList}"> <#tr class="3c" name="name"> 名称 </#t

html --- ajax --- javascript --- 简单的封装

Ajax的简单封装 Ajax的全称是AsynchronousJavaScriptAndXML 如有疑问请参考:http://zh.wikipedia.org/zh-cn/AJAX 以及传智播客的视频教程:http://java.itcast.cn/news/8308d118/740a/4dcd/8dbf/c73d2fe5bc60.shtml 其实已经有好多框架对它进行了封装,但是还是有好多时候用到自己封装的 透露一下,本人是宇多田光的听众之一哦! 封装后的代码如下: 文件路径:\web\Ajax

bootstrap modal垂直居中

使用过bootstrap modal(模态框)组件的人都有一种困惑, 好好的一个弹出框怎么就无法垂直居中了呢? 当然网上一些前辈也给出了不少答案, 感觉不太全而且针对的都是各自的项目难以给我等小白太直观的理解.因而手痒试试写个稍微完整点的解决方案, 作为总结及日后回顾之用. 项目中的bootstrap版本是3.X , 作为项目后台使用. 在项目进行过程中遇到组件弹出框无法垂直居中,示例demo代码如下: <!DOCTYPE html> <html> <head> <

扩展封装暴雪哈希算法(blizard hash algorithm),并与STL map进行操作性能上的比较

问题描述: 1.blizard hash algorithm 是众所周知的算法,关于它极小的碰撞概率和实现的简洁性一直为热爱技术的朋友津津乐道: 2.blizard hash algorithm 有个致命的问题就是它的实现受制于一个固定的(预先开辟的buffer)的限制,暴雪给出的是1024,也即当hash table 的填充的元素(key value pair)查过1024时,就没办法再往里面进行key value 对填充,这极大的限制了它的使用.在实现的应用,我们经常会向hash table

递归扩展变量和简单扩展变量

1 递归扩展变量和简单扩展变量的区别 最大的区别在于,递归扩展变量等号右边的表达式里面的变量要等到该递归变量被用的时候再去扩展,而简单变量的话,在读取makefile的时候就已经扩展了. 并且简单扩展变量有前后关系,简单扩展变量只能用到在其前面定义的变量,而不能用到在其后面定义的变量.而递归扩展变量,要用的时候,需要扩展的时候,可以一直扩展到不能扩展为止,即递归扩展. 2 例子 foo := $(bar) bar = aaa all:;echo $(foo) 输出为空,因为在给foo赋值扩展等号

Bootstrap modal常用参数、方法和事件

Bootstrap modal(模态窗)常用参数.方法和事件: 参数: 名称 类型 默认值 描述 Backdrop Boolean或字符串"static" True True:有背景,点击modal外部,modal消失. False:无背景,点击modal外部,modal不消失. Static:有背景,点击modal外部,modal不消失. Keyboard Boolean True True:键盘上的esc按下关闭modal False:键盘上的esc按下不关闭modal Show