Bootstrap3 模态框使用实例
程序员文章站
2023-11-30 09:19:10
不支持同时打开多个模态框
务必尽量将html代码放置在模态框的body位置以避免其他组件影响模态框
*autofocus 对于模态框无效, 需要自己调用 $('...
不支持同时打开多个模态框
务必尽量将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 模态框使用实例,希望对大家有所帮助
上一篇: JS验证字符串功能
下一篇: js canvas实现写字动画效果