首页 > 编程 > JavaScript > 正文

Bootstrap3 模态框使用实例

2019-11-19 17:27:22
字体:
来源:转载
供稿:网友

不支持同时打开多个模态框

务必尽量将HTML代码放置在模态框的body位置以避免其他组件影响模态框

*autofocus 对于模态框无效, 需要自己调用 $('#myModal').on('shown.bs.modal', function () { $('#myInput').focus()})*

实例

静态

<div class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content">  <div class="modal-header">  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>  <h4 class="modal-title">Modal title</h4>  </div>  <div class="modal-body">  <p>One fine body…</p>  </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 changes</button>  </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --></div><!-- /.modal -->

动态

<!-- Button trigger modal --><button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal</button><!-- Modal --><div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content">  <div class="modal-header">  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>  <h4 class="modal-title" id="myModalLabel">Modal title</h4>  </div>  <div class="modal-body">  ...  </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 changes</button>  </div> </div> </div></div>

可选尺寸

<!-- Large modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Large modal</button><div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content">  ... </div> </div></div><!-- Small modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">Small modal</button><div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"> <div class="modal-dialog modal-sm" role="document"> <div class="modal-content">  ... </div> </div></div>

禁用fade

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..."> ...</div>

使用栅格系统

仅需在body中使用 .rows

<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content">  <div class="modal-header">  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>  <h4 class="modal-title" id="gridSystemModalLabel">Modal title</h4>  </div>  <div class="modal-body">  <div class="row">   <div class="col-md-4">.col-md-4</div>   <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>  </div>  <div class="row">   <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>   <div class="col-md-2 col-md-offset-4">.col-md-2 .col-md-offset-4</div>  </div>  <div class="row">   <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>  </div>  <div class="row">   <div class="col-sm-9">   Level 1: .col-sm-9   <div class="row">    <div class="col-xs-8 col-sm-6">    Level 2: .col-xs-8 .col-sm-6    </div>    <div class="col-xs-4 col-sm-6">    Level 2: .col-xs-4 .col-sm-6    </div>   </div>   </div>  </div>  </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 changes</button>  </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --></div><!-- /.modal -->

使用方法

通过data属性

<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>

通过JavaScript调用

$('#myModal').modal(options)

参数

名称 类型 默认值 描述
backdrop boolean 或 字符串 ‘static' true 设置为 static 时, 在点击屏幕时不会关闭
keyboard boolean true 键盘上的 esc 键被按下时关闭模态框
show boolean true 模态框初始化之后就立即显示出来

方法

模态框激活

.modal(options)
$('#myModal').modal({ keyboard: false})

模态框触发手动打开或者关闭

触发在 shown.bs.modal 以及 hidden.bs.modal 事件之前

.modal('toggle')

手动打开模态框

触发在 shown.bs.modal 之前

$('#myModal').modal('show')

手动隐藏模态框

触发在 hidden.bs.modal 之前

$('#myModal').modal('hide')

事件

按照时间先后顺序分别为 show.bs.modal shown.bs.modal hide.bs.modal  hidden.bs.modal

调用方式

$('#myModal').on('hidden.bs.modal', function (e) { // do something...})

以上所述是小编给大家介绍的Bootstrap3 模态框使用实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表