BootStrap modal模态弹窗使用

- 日理万妓 2021-09-14 10:22 576阅读 0赞

默认的model案例:

  1. <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Modal</title> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <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"> <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</button> </div> </div> </div> </div> <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </body> </html>
  2. 一.model打开

1、静态打开:通过data属性打开隐藏模态框
设置按钮button的data-toggle:”modal”(以模态框的形式打开),data-target:”#myModal”(设置为modal的id)
2、动态打开:以jquery代码为例

$(“#myModal”).modal(“toggle”);

二.model的设置

1.通过modal-sm和modal-lg改变弹窗的大小,这两个class要设置在modal-dialog那一层
2.modal-header中的关闭按钮的关键属性为class=”close”该class实现了样式的改变,data-dismiss=”modal”提供了HTML关闭的触发条件
3.模态弹窗提供了四个属性,这四个属性通常设置在模态弹窗上class=”modal”那一层(内容DOM的最外层),

四个属性分别为:
a.data-backdrop

是否包含一个背景,默认值为true同时单击背景可以关闭模态窗,设置为data-backdrop=”static”则单击背景时不关闭,设置为backdrop=”false”则不存在背景

b.data-keyboard

按下ESC时是否关闭模态窗默认值为true即按下时关闭模态窗,设置为data-keyboard=”false”则在点击ESC时不再关闭模态窗(该属性要想生效要在最外层设置属性tabindex)

c.data-show

初始化时是否显示默认值为true即初始时显示,data-show=”false”则初始化时不显示第一次点击触发元素准备模态窗,在点击一次开始显示模态窗

d.href:加载其他内容

三.model 模态窗提供了四个事件:

事件类型 描述
show.bs.modalshow 方法调用之后立即触发该事件。如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问。
shown.bs.modal此事件在模态框已经显示出来(并且同时在 CSS 过渡效果完成)之后被触发。如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问。
hide.bs.modal hide 方法调用之后立即触发该事件。
hidden.bs.modal此事件在模态框被隐藏(并且同时在 CSS 过渡效果完成)之后被触发。
loaded.bs.modal从远端的数据源加载完数据之后触发该事件。

注意:

1.show.bs.modal在显示之前触发
2.shown.bs.modal在显示之后触发
3.hide.bs.modal在隐藏之前触发
4.hidden.bs.modal在隐藏之后触发

  1. modal框加载同时,提供几个方法用来控制modal $("#myModal").on("loaded.bs.modal",function{ //在模态框加载的同时做一些动作 }); $("#myModal").on("show.bs.modal",function{ //在show方法后调用 }); $("#myModal").on("shown.bs.modal",function{ //在模态框完全展示出来做一些动作 }); $("#myModal").on("hide.bs.modal",function{ //hide方法后调用 }); $("#myModal").on("hiden.bs.modal",function{ //监听模态框隐藏事件做一些动作 });

列如:

  1. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 实例 - 模态框(Modal)插件</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <button class="btn btn-primary btn-lg"> 开始演示模态框 </button> <!-- 模态框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel"> 模态框(Modal)标题 </h4> </div> <div class="modal-body"> 在这里添加一些文本 </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭 </button> <button type="button" class="btn btn-primary"> 提交更改 </button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> <script> $(function(){ $(".btn").click(function(){ $("#myModal").modal("toggle"); }); $('#myModal').on('show.bs.modal', function (e) { alert('show.bs.modal') }); $('#myModal').on('shown.bs.modal', function (e) { alert('shown.bs.modal') }) }); </script> </body> </html>

列如:当有一堆按钮,都要触发相同的模态框(如:向好友列表中某个人发消息),只是有用户ID不同,那么可以使用data-whatever配合event.relatedtarget来实现:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Bootstrap Modal</title>
  8. <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
  9. </head>
  10. <body>
  11. <div class="panel panel-default">
  12. <div class="panel-heading">好友列表</div>
  13. <div class="panel-body">
  14. <div class="list-group" role="group" aria-label="好友列表">
  15. <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
  16. data-whatever="张三">张三
  17. </button>
  18. <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
  19. data-whatever="李四">李四
  20. </button>
  21. <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
  22. data-whatever="王二">王二
  23. </button>
  24. </div>
  25. </div>
  26. </div>
  27. <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  28. <div class="modal-dialog" role="document">
  29. <div class="modal-content">
  30. <div class="modal-header">
  31. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
  32. aria-hidden="true">×</span></button>
  33. <h4 class="modal-title" id="exampleModalLabel">New message</h4>
  34. </div>
  35. <div class="modal-body">
  36. <form>
  37. <div class="form-group">
  38. <label for="recipient-name" class="control-label">Recipient:</label>
  39. <input type="text" class="form-control" id="recipient-name">
  40. </div>
  41. <div class="form-group">
  42. <label for="message-text" class="control-label">Message:</label>
  43. <textarea class="form-control" id="message-text"></textarea>
  44. </div>
  45. </form>
  46. </div>
  47. <div class="modal-footer">
  48. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  49. <button type="button" class="btn btn-primary">Send message</button>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
  55. <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  56. <script>
  57. $('#exampleModal').on('show.bs.modal', function (event) {
  58. var button = $(event.relatedTarget) // 触发事件的按钮
  59. var recipient = button.data('whatever') // 解析出data-whatever内容
  60. var modal = $(this)
  61. modal.find('.modal-title').text('Message To ' + recipient)
  62. modal.find('.modal-body input').val(recipient)
  63. })
  64. </script>
  65. </body>
  66. </html>

发表评论

表情:
评论列表 (有 0 条评论,576人围观)

还没有评论,来说两句吧...

相关阅读