最近用上了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. C# 托管

    委托 委托让我们可以把函数引用保存在变量中.这就像在 C++ 中使用 typedef 保存函数指针一样. 委托使用关键字 delegate 声明.看看这个例子,你就能理解什么是委托: 例子: 代码: ...

  2. 50道JAVA基础编程练习题 - 题目

    50道JAVA基础编程练习题[1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? [2]题目:判断 ...

  3. c++ get the pointer from the reference

    int x = 5; int& y = x; int* xp = &x; int* yp = &y; xp is equal to yp. 也就是说,直接对reference取 ...

  4. kafka source type

    https://flume.apache.org/FlumeUserGuide.html # example.conf: A single-node Flume configuration # Nam ...

  5. Python爬虫-- BeautifulSoup库

    BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...

  6. Mysql 外键级联

    如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...

  7. Java分支循环结构

    一.Java分支结构 1.if语句:一个 if 语句包含一个布尔表达式和一条或多条语句. if 语句的用语法如下:  if(布尔表达式){ 如果布尔表达式为true将执行的语句  } public c ...

  8. A. Drazil and Date

    这是codeforces#292 div2 的一道题,因为本人比较水,目前只能做div2了.问题简化版就是: 从 (0,0) 走到 (a, b) ,s 步能不能走完.每次能向上下左右走,且只能走一步. ...

  9. webpack热替换原理

    前期准备: const path = require('path'); const HtmlWebpackPlugin= require('html-webpack-plugin'); const C ...

  10. media server died的解决方法【转】

    本文转载自:https://blog.csdn.net/class_brick/article/details/78086261 在对MediaRecord进行初始化时添加上 mMediaRecord ...