Bootstrap入门(二十三)JS插件1:模态框

1.静态实例

2.动态实例

3.模态框的尺寸和效果

4.包含表单的模态框

模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能集。

但是千万不要在一个模态框上重叠另一个模态框。要想同时支持多个模态框,需要自己写额外的代码来实现。

首先我们要引入CSS文件和JS文件(bootstrap需要jQuery的支持)

<link href="bootstrap.min.css" rel="stylesheet">
<script src="jquery-3.1.0.min.js" type="text/javascript"></script>
<script src="bootstrap.min.js" type="text/javascript"></script>

1.静态实例

首先我们要创建一个class为modal的div

在里面再创建一个class为modal-dialog的div(这才是模态框)

模态框是分为头部,身体,和脚部

再创建一个class为modal-content的div,为内容部分

我们再创建一个身体部分,假设内容为hello world

        <div class="modal" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallable" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
hello world
</div>
</div>
</div>
</div>

当然,如果直接这样的话是没有任何效果的,这就是我们需要JS的时候

添加一段新的js代码(这里是用到了模态框代码的id:mymodal,可以根据自己需要修改)

(注意:使用id时,需要在前面加上“#”,不然同样是没有效果的)

     <script>
$("#mymodal").modal("show");
</script>

这样,我们就有效果了,背景变成灰色,弹出模态框

当然,这跟我们平时看到的不一样,那是因为还缺少了头部和脚部

补全代码,用<span>来承载那个“X”,用按钮的不同情境来显示效果

        <div class="modal" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallable" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="model-title" id="mymodallabel">这里是头部</h4>
</div>
<div class="modal-body">
hello world
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">close</button>
<button type="button" class="btn btn-primary">save</button>
</div>
</div>
</div>
</div>

刷新页面,新弹出的模态框,可以按“X”或者按钮close关闭

效果:

2.动态实例

静态实例是一进入页面就弹出来的,可是一般都不会这样,

因为事实上肯定是通过一些按钮,控件来触发的

我们添加一个按钮(这里是一个正常、稍大的样式)

注意按钮里面的data-target="#mymodal"

     <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mymodal">
启动模态框
</button>

刷新页面,在点击按钮后,弹出模态框,效果

3.模态框的尺寸和效果

模态框提供了三个可选尺寸(有大的,默认的,小的),通过为 .modal-dialog 增加一个样式调整类实现

同时也有份有过渡效果和没过渡效果的(只需要在class中添加或者去掉fade就可以了)

2里面的就是默认的,接下来,假设:大的,有过度效果;小的,没有过度效果

为方便,只写一点

     <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">
启动大模态框
</button>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="mylargemodallabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">大 有过渡</div>
</div>
</div> <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">
启动小模态框
</button>
<div class="modal bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mylargemodallabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">小 无过渡</div>
</div>
</div>

效果,大的明显大很多,有过渡效果的是渐渐出现的,没有过度效果的是突然出现的

4.包含表单的模态框

依然是用按钮来触发,在按钮的属性中,注意data-target="#examplemodal" 和data-whatever="@ding"

data-target="#examplemodal"是对模态框所在div的调用

data-whatever="@ding"是指获取的内容是@ding

<h4>标签也有自己的id:examplemodallable

        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#examplemodal" data-whatever="@ding">
open modal for @ding
</button>
<div class="modal fade" id="examplemodal" tabindex="-1" role="dialog" aria-labelledby="examplemodallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="examplemodallable">message</h4>
</div>
<div class='modal-body'>
<form>
<div class='form-group'>
<label for='recipient-name' class='control-label'>ASD</label>
<input class="form-control" type="text" id='recipient-name'>
</div>
</form>
</div>
<div class='modal-footer'>
<button type="button" class='btn btn-default' data-dismiss='modal'>close</button>
<button type="button" class='btn btn-primary'>send</button>
</div>
</div>
</div>
</div>

当然,现在的效果不是我们想要的,现在的效果是:

想把data-whatever的内容我们来添加到表单中,新增梯段js代码:

前面是定义变量获取相应的信息

modal.find(".modal-title").text(AAA);(这是头部内容)

modal.find(".modal-body input").val(recipient))(修改到身体部分的<input>中)

        <script>
$("#examplemodal").on("show.bs.modal", function (event) {
var button = $(event.relatedTarget)
var recipient = button.data("whatever")
var modal = $(this);
modal.find(".modal-title").text("new message to" + recipient);
modal.find(".modal-body input").val(recipient)
})</script>

刷新页面,重新点击,头部的信息和<input>里面的信息发生了变化

这就很类似用户提示,能提高用户体验

Bootstrap入门(二十三)JS插件1:模态框的更多相关文章

  1. 使用bootstrap的JS插件实现模态框效果

    在上一篇文章中,我们使用 js+css 实现了模态框效果,在理解了模态框的基本实现方法和实现效果后,我们就要寻找更快捷的方法,又快又好的来完成模态框开发需求,从而节约时间,提高效率.一个好的轮子,不仅 ...

  2. Bootstrap <基础二十三>页面标题(Page Header)

    页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...

  3. 使用bootstrap的插件实现模态框效果

    在上一篇文章中,我们使用 js+css 实现了模态框效果,在理解了模态框的基本实现方法和实现效果后,我们就要寻找更快捷的方法,又快又好的来完成模态框开发需求,从而节约时间,提高效率.一个好的轮子,不仅 ...

  4. Bootstrap入门(十三)组件7:导航条

    Bootstrap入门(十三)组件7:导航条 1.默认样式的导航条 2.嵌入表单和按钮 3.嵌入文本和非导航的链接 4.组件排列和下拉菜单 5.固定在顶部/底部 6.反色的导航条 7.路径导航 首先先 ...

  5. Bootstrap的js插件之警告框(alert.js)

    data-dismiss="alert"--为关闭button加入该属性能够使其自己主动为警告框赋予关闭功能. .fade .in--为警告框在关闭时加入动画效果. 很多其它细节參 ...

  6. BootStrap母版页布局.子页面布局.BootstrapTable.模态框.警告框.html导出tabl生成Excel.HTML生成柱图.饼图.时间控件中文版

    如上就是很多后台管理系统的母版页布局. 左边一列模板.上面一列系统标识. 空白处充填子页面 以ASP.NET MVC为基础 引入bootstrap.js.bootstrap.css body: < ...

  7. JS学习笔记(模态框JS传参)

    博主最近基于django框架的平台第一版差不多完成了 今天整理下开发过程中遇到的前端知识 基于前端bootstrap框架模态框传参问题 上前端html代码: <div class="m ...

  8. JS /CSS 实现模态框(注册和登录组件)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. JS 实现图片模态框,幻灯片,跑马灯功能

    网站中常用的幻灯片和模态框,使用 HTML.JavaScript 与 CSS 来创建 Lightbox,类似相册预览功能.可以运用到视频网站,商城,相册上去 参考了菜鸟教程,有兴趣自己去看 HTML/ ...

  10. bootstrap table编辑操作的时候 在模态框里加载iframe页面(加载的页面是在另一个页面做编辑)的时候如何关闭模态框和刷新table

    //关闭模态框                             window.parent.$('#myModal').modal('hide'); //修改成功后刷新table表格      ...

随机推荐

  1. Android——apk反编译

    一.工具准备: 1.dex2jar:http://code.google.com/p/dex2jar/downloads/list 2.JD-GUI:windows:http://laichao.go ...

  2. Android启动脚本init.rc(2)

    在Android中使用启动脚本init.rc,可以在系统的初始化中进行简单的操作. init.rc启动脚本路径:system/core/rootdir/init.rc 内容: Commands:命令 ...

  3. Windows系统字体与文件对照表

    源:Windows系统字体与文件对照表 宋体 (TrueType) = SIMSUN.TTF 黑体 (TrueType) = simhei.ttf 楷体_GB2312 (TrueType) = sim ...

  4. SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

    #import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+Web ...

  5. C# MODBUS协议 上位机(转)

    源:C# MODBUS协议 上位机 C#写了一款上位机监控软件,基于MODBUS_RTU协议. 软件的基本结构: 采用定时器(Timer控件)为时间片. 串口采用serialPort1_DataRec ...

  6. 五、pig学习

    一.什么是pig 1.pig和sql.map-reduce的关系 来自为知笔记(Wiz)

  7. python继承的实例

    class SchoolMember(object):#定义学校 member=0#默认成员为0个 amount=0#默认学费为0元 def __init__(self,name,age,sex):# ...

  8. SocketChannel

    Java NIO 中的 SocketChannel 是一个连接到 TCP 网络套接字的通道.可以通过以下 2 种方式创建 SocketChannel: 打开一个 SocketChannel 并连接到互 ...

  9. IOS开发使用YiRefresh进行刷新

    1.将YiRefresh下载后,拖进项目 YiRefresh地址:https://github.com/coderyi/YiRefresh 2.添加两个头文件 #import "YiRefr ...

  10. leetcode--009 Linked List Cycle I

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZgAAACACAIAAAC5q+hAAAAJ+UlEQVR4nO2dwbXrKBJAOyelRShEQw