最近用上了bootstrap这个强大的前端框架,有空来总结一下。这里记录下模态框的简单应用。

首先,要在页面中引入相应的js、css文件

 <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script> //这里如果只用到bootstrap的模态框的话,可以换成model.js
<script type="text/javascript" src="js/jquery-ui-custom.min.js"></script> //这个js里主要是需要用到jquery.ui.draggable.js,但是这个js需要依赖其他的js,所以我直接上jquery-ui的官网上根据自己的需要生成

然后在html里写一个模态框的实例,内容写在class="modal-body"这个div里。

 <!-- 点击触发模态框 -->
<button id="demo_button" type="button" class="btn btn-default" data-toggle="modal" data-target="#demoModal" style="width:80px;height:36px;">点击我</button>
<!-- 模态框(Modal)-->
<div class="modal fade" id="demoModal" tabindex="-1" role="dialog" aria-labelledby="demoModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog" style="width: 800px;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="demoModalLabel">这是标题</h4>
</div>
<div class="modal-body" style="height: 320px;">
<form action="" method="post" id="userForm"> </form>
</div><!-- /.modal-body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" onclick="saveOrUpdateUser()">提交更改</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>

触发modal有2种方式,一种就是上面给div添加2个属性  data-toggle="modal" data-target="#demoModal" 或者 href="#demoModel" 其中#demoModel是要弹出的模态框的id。另一种方式是用js控制:$('#demoModal').modal('show');

说一下一些比较重要的属性:

id:模态框的id。
  aria-labelledby:该属性引用模态框的标题
  aria-hidden:true 用于保持模态窗口不可见,直到触发器被触发为止,比如上面的button
  data-backdrop:static 表示点击遮罩层不关闭模态窗口


这样子,一个基本的bootstrap模态框就写好了,但是现在的模态框只是水平居中,而且是不能拖拽的,所以还要进行一些处理。

 //设置模态框可移动 这里用到上面引入的jquery-ui-custom.min.js
$(#id).draggable({
handle: ".modal-header",
cursor: 'move',
refreshPositions: false
}); //模态框居中显示
function centerModals() {
$(#id).each(function(i){
var $clone = $(this).clone().css('display', 'block').appendTo('body');
//设置modal垂直居中
var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
top = top > 0 ? top : 0;
$(this).find('.modal-content').css("margin-top", top);
$clone.remove(); });
} $(window).on('resize', centerModals);

还要修改bootstrap.css中的一个样式

 .modal-backdrop {
position: absolute;
top:;
right:;
left:;
background-color: #000;
}

改为:

 .modal-backdrop {
position: fixed;
top:;
right:;
left:;
bottom:;
background-color: #000;
}

这里是一些可与 modal() 一起使用的有用的方法。


这里是一些可用的事件

由于我需要用到很多不同的模态框,打开和关闭的时候都需要执行一些动作,所以稍微做了下封装。。。

 /**
* 初始化模态窗口
* @param modalName 模态窗口id
* @param showBcak show时执行的方法
* @param hideBcak hide时执行的方法
*/
function modalInit(modalName,showBcak,hideBcak)
{
var modalName = '#' + modalName;
//设置模态框可移动
$(modalName).draggable({
handle: ".modal-header",
cursor: 'move',
refreshPositions: false
}); //模态框居中显示
function centerModals() {
$(modalName).each(function(i){
var $clone = $(this).clone().css('display', 'block').appendTo('body');
//设置modal垂直居中
var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
top = top > 0 ? top : 0;
$(this).find('.modal-content').css("margin-top", top);
$clone.remove(); });
}
//调用show方法时触发
$(modalName).on('show.bs.modal', function(){
if (null != showBcak && "" != showBcak) {
var funcBack = eval(showBcak);
new funcBack();
}
centerModals();
});
//调用hide方法时触发
$(modalName).on('hide.bs.modal', function(){
if (null != hideBcak && "" != hideBcak)
{
var funcBack = eval(hideBcak);
new funcBack();
} });
$(window).on('resize', centerModals);
}

调用

 modalInit("demoModal", null,null);

附件:

  http://files.cnblogs.com/files/zzd-zxj/model.rar

参考资料:

  Bootstrap模态框水平垂直居中与增加拖拽功能
http://www.panshy.com/articles/201509/webdev-2524.html
  Bootstrap 模态框(Modal)插件
http://www.dnzs.com.cn/w3cschool/bootstrap/bootstrap-modal-plugin.html

JavaScript:bootstrap 模态框的简单应用的更多相关文章

  1. Bootstrap 模态框(Modal)插件

    原文链接:http://www.runoob.com/bootstrap/bootstrap-modal-plugin.html Bootstrap 模态框(Modal)插件 模态框(Modal)是覆 ...

  2. 使用bootstrap模态框实现浮动层

    authour: 陈博益 updatetime: 2015-04-22 06:52:15 friendly link: http://v3.bootcss.com/javascript/#modals ...

  3. Bootstrap模态框(MVC)

    BZ这篇博客主要是为大家介绍一下MVC如何弹出模态框.本文如果有什么不对的地方,希望大神们多多指教,也希望和我一样的小菜多多学习.BZ在这里谢过各位. 首先要在页面加上一个点击事件: @Html.Ac ...

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

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

  5. BootStrap 模态框基本用法

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态框(Modal)插件方法</title ...

  6. JavaScript插件——模态框

    Bootstrap3.0学习第十七轮(JavaScript插件——模态框)   前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/ ...

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

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

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

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

  9. bootstrap模态框传值操作

    1.bootstrap模态框之html代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...

随机推荐

  1. js获取和设置属性的方法

    function square(num){    var total = num*num;//局部变量    return total;}var total = 50;//全局变量var number ...

  2. 本地filezilla&amp;servervsftp搭配使用

    环境:本地ubuntu系统&serverubuntu系统 本地安装filezilla  apt-get install filezilla '安装filezilla filezilla '执行 ...

  3. WinForm程序打包工具InnoSetup使用说明图文教程

    WinForm程序打包工具InnoSetup使用说明图文教程 WinForm程序开发测试好了,如果将Debug/Release里面的文件发给客户使用,会让客户觉得你不够专业,但是使用VS自带的打包工具 ...

  4. Java多线程面试问题

    这篇文章主要是对多线程的面试问题进行总结的,罗列了40个多线程的问题. 1. 多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓 ...

  5. 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包

    [BZOJ2427][HAOI2010]软件安装 Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为 ...

  6. 【BZOJ4241】历史研究 分块

    [BZOJ4241]历史研究 Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开 ...

  7. WebService 简单应用

    Web服务全称:XML Web Service,是一种可编程的应用程序逻辑组件,它可以在Internet或企业网的Web应用程序之间共享.Web服务被设计成能够通过Internet与其它应用程序之间直 ...

  8. the max number of open files 最大打开文件数 ulimit -n RabbitMQ调优

    Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) — RabbitMQ https://www.rabbitmq.com/i ...

  9. Python解释器是单线程应用 IO 密集型 计算密集型 GIL global interpreter lock

    [Python解释器是单线程应用] [任意时刻,仅执行一个线程] 尽管Python解释器中可以运行多个线程,但是在任意给定的时刻只有一个线程会被解释器执行. [GIL锁 保证同时只有一个线程运行] 对 ...

  10. Hadoop实战-Flume之Source multiplexing(十五)

    a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...