本篇文章为大家展示了如何正确的使用Bootstrap3 模态框,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
不支持同时打开多个模态框
务必尽量将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> <h5 class="modal-title">Modal title</h5> </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> <h5 class="modal-title" id="myModalLabel">Modal title</h5> </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> <h5 class="modal-title" id="gridSystemModalLabel">Modal title</h5> </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 模态框,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。