8-6 模态弹出框--实现原理解析(二)
本节编程练习不计算学习进度,请电脑登录imooc.com操作

模态弹出框--实现原理解析(二)

蒙板样式实现:

大家或许注意到了,在做模态弹出窗时,底部常常会有一个透明的黑色蒙层效果,如下图所示:

在Bootstrap框架中为模态弹出窗也添加了一个这样的蒙层样式“modal-backdrop”,只不过他默认情况下是全屏黑色的:

/*bootstrap.css文件第5424行~第5432行*/
.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1040;
  background-color: #000;
}

同样,给其添加了一个过渡动画,从fade到in,把opacity值从0变成了0.5。上图展示的就是in状态下的效果:

/*bootstrap.css文件第5433行~第5440行*/
.modal-backdrop.fade {
  filter: alpha(opacity=0);
  opacity: 0;
}
.modal-backdrop.in {
  filter: alpha(opacity=50);
  opacity: .5;
}

两种尺寸选择:

除此之外,Bootstrap框架还为模态弹出窗提供了不同尺寸,一个是大尺寸样式“modal-lg”,另一个是小尺寸样式“modal-sm”。其结构上稍做调整:

<!-- 大尺寸模态弹出窗 -->
<div class="modal fade bs-example-modal-lg" tabindex="-1"role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <divclass="modal-dialog modal-lg">
       <divclass="modal-content"> ... </div>
    </div>
</div>
<!-- 小尺寸模态弹出窗 -->
<divclass="modal fade bs-example-modal-sm"tabindex="-1"role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <divclass="modal-dialog modal-sm">
       <divclass="modal-content"> ... </div>
    </div>
</div>

来简单的看一个示例效果:

对于这两种尺寸的模态弹出窗,Bootstrap在媒体查询中做过处理:代码同上见bootstrap.css

任务

我来试试:在右侧补充代码,把右侧弹出窗修改为“小尺寸”

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>模态弹出框--实现原理解析(二)</title>
  6. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <h3>大/小尺寸模态弹出框</h3>
  10. <!-- Large modal -->
  11. <button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">
  12. 大的模态弹出窗
  13. </button>
  14.  
  15. <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  16. <div class="modal-dialog modal-lg">
  17. <div class="modal-content">
  18. <div class="modal-header">
  19. <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  20. <h4 class="modal-title">模态弹出窗标题</h4>
  21. </div>
  22. <div class="modal-body">
  23. <p>模态弹出窗主体内容</p>
  24. </div>
  25. <div class="modal-footer">
  26. <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  27. <button type="button" class="btn btn-primary">保存</button>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32.  
  33. <!-- Small modal -->
  34. <button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">
  35. 小的模态弹出窗
  36. </button>
  37.  
  38. <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  39. <div class="modal-dialog">
  40. <div class="modal-content">
  41. <div class="modal-header">
  42. <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  43. <h4 class="modal-title">模态弹出窗标题</h4>
  44. </div>
  45. <div class="modal-body">
  46. <p>模态弹出窗主体内容</p>
  47. </div>
  48. <div class="modal-footer">
  49. <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  50. <button type="button" class="btn btn-primary">保存</button>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55.  
  56. <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
  57. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  58. </body>
  59. </html>
下一节