8-4 模态弹出框--结构分析
本节编程练习不计算学习进度,请电脑登录imooc.com操作

模态弹出框--结构分析

Bootstrap框架中的模态弹出框,分别运用了“modal”、“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在“modal-content”中,其主要又包括三个部分:

  ☑ 弹出框头部,一般使用“modal-header”表示,主要包括标题和关闭按钮

  ☑ 弹出框主体,一般使用“modal-body”表示,弹出框的主要内容

  ☑ 弹出框脚部,一般使用“modal-footer”表示,主要放置操作按钮

如下图所示:

模态弹出窗的结构如下:

<div class="modal show">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">模态弹出窗标题</h4>
            </div>
            <div class="modal-body">
                <p>模态弹出窗主体内容</p>
            </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-dialog -->
</div><!-- /.modal -->

弹出窗的主体样式实现:

但是对于一个模态弹出窗而言,modal-content才是样式的关键。其主要设置了弹出窗的边框边距背景色阴影等样式。

/*bootstrap.css文件第5412行~第5423行*/
.modal-content {
  position: relative;
  background-color: #fff;
  -webkit-background-clip: padding-box;
          background-clip: padding-box;
  border: 1px solid #999;
  border: 1px solid rgba(0, 0, 0, .2);
  border-radius: 6px;
  outline: 0;
  -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, .5);
          box-shadow: 0 3px 9px rgba(0, 0, 0, .5);
}

除此之外,modal-content中的modal-header、modal-body和modal-footer三个部分样式设置:

/*bootstrap.css文件第5441行~第5461行*/
.modal-header {
  min-height: 16.42857143px;
  padding: 15px;
  border-bottom: 1px solid #e5e5e5;
}
.modal-header .close {
  margin-top: -2px;
}
.modal-title {
  margin: 0;
  line-height: 1.42857143;
}
.modal-body {
  position: relative;
  padding: 15px;
}
.modal-footer {
  padding: 15px;
  text-align: right;
  border-top: 1px solid #e5e5e5;
}

这三个部分主要控制一些间距的样式。而modal-footer都是用来放置按钮,所以底部还对包含的按钮做了一定的样式处理。

/*bootstrap.css文件第5462行~第5471行*/
.modal-footer .btn + .btn {
  margin-bottom: 0;
  margin-left: 5px;
}
.modal-footer .btn-group .btn + .btn {
  margin-left: -1px;
}
.modal-footer .btn-block + .btn-block {
  margin-left: 0;
}

 

 

任务

  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. <div class="modal show">
  10. <div class="modal-dialog">
  11. <div class="modal-content">
  12. <div class="modal-header">
  13. <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  14. <h4 class="modal-title">模态弹出窗标题</h4>
  15. </div>
  16. <div class="modal-body">
  17. <p>模态弹出窗主体内容</p>
  18. </div>
  19. <div class="modal-footer">
  20. <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  21. <button type="button" class="btn btn-primary">保存</button>
  22. </div>
  23. </div><!-- /.modal-content -->
  24. </div><!-- /.modal-dialog -->
  25. </div><!-- /.modal -->
  26. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
  27. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  28. </body>
  29. </html>
  1. /*模态弹出窗是固定在浏览器中的: */
  2. /*bootstrap.css文件第5379行~第5389行*/
  3. .modal {
  4. position: fixed;
  5. top: 0;
  6. right: 0;
  7. bottom: 0;
  8. left: 0;
  9. z-index: 1050;
  10. display: none;
  11. overflow: hidden;
  12. -webkit-overflow-scrolling: touch;
  13. outline: 0;
  14. }
  15.  
  16. /*给.modal增加类名fade添加了一个过渡动画效果:*/
  17. /*bootstrap.css文件第5390行~第5402行*/
  18. .modal.fade .modal-dialog {
  19. -webkit-transition: -webkit-transform .3s ease-out;
  20. -o-transition: -o-transform .3s ease-out;
  21. transition: transform .3s ease-out;
  22. -webkit-transform: translate3d(0, -25%, 0);
  23. -o-transform: translate3d(0, -25%, 0);
  24. transform: translate3d(0, -25%, 0);
  25. }
  26. .modal.in .modal-dialog {
  27. -webkit-transform: translate3d(0, 0, 0);
  28. -o-transform: translate3d(0, 0, 0);
  29. transform: translate3d(0, 0, 0);
  30. }
  31.  
  32. /*在全屏状态下,模态弹出窗宽度是自适应的,而且modal-dialog水平居中:*/
  33. /*bootstrap.css文件第5407行~第5411行*/
  34. .modal-dialog {
  35. position: relative;
  36. width: auto;
  37. margin: 10px;
  38. }
  39.  
  40. /*浏览器视窗大于768px时,模态弹出窗的宽度为600px:*/
  41. /*bootstrap.css文件第5479行~第5491行*/
  42. @media (min-width: 768px) {
  43. .modal-dialog {
  44. width: 600px;
  45. margin: 30px auto;
  46. }
  47. .modal-content {
  48. -webkit-box-shadow: 0 5px 15px rgba(0, 0, 0, .5);
  49. box-shadow: 0 5px 15px rgba(0, 0, 0, .5);
  50. }
  51. .modal-sm {
  52. width: 300px;
  53. }
  54. }
  55.  
  56. /*对于一个模态弹出窗而言,modal-content才是样式的关键:*/
  57. /*bootstrap.css文件第5412行~第5423行*/
  58. .modal-content {
  59. position: relative;
  60. background-color: #fff;
  61. -webkit-background-clip: padding-box;
  62. background-clip: padding-box;
  63. border: 1px solid #999;
  64. border: 1px solid rgba(0, 0, 0, .2);
  65. border-radius: 6px;
  66. outline: 0;
  67. -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, .5);
  68. box-shadow: 0 3px 9px rgba(0, 0, 0, .5);
  69. }
  70.  
  71. /*modal-content还包括了modal-header、modal-body和modal-footer三个部分:*/
  72. /*bootstrap.css文件第5441行~第5461行*/
  73. .modal-header {
  74. min-height: 16.42857143px;
  75. padding: 15px;
  76. border-bottom: 1px solid #e5e5e5;
  77. }
  78. .modal-header .close {
  79. margin-top: -2px;
  80. }
  81. .modal-title {
  82. margin: 0;
  83. line-height: 1.42857143;
  84. }
  85. .modal-body {
  86. position: relative;
  87. padding: 15px;
  88. }
  89. .modal-footer {
  90. padding: 15px;
  91. text-align: right;
  92. border-top: 1px solid #e5e5e5;
  93. }
  94.  
  95. /*modal-footer都是用来放置按钮,现在底部还对包含的按钮做一定的样式处理:*/
  96. /*bootstrap.css文件第5462行~第5471行*/
  97. .modal-footer .btn + .btn {
  98. margin-bottom: 0;
  99. margin-left: 5px;
  100. }
  101. .modal-footer .btn-group .btn + .btn {
  102. margin-left: -1px;
  103. }
  104. .modal-footer .btn-block + .btn-block {
  105. margin-left: 0;
  106. }
  107.  
  108. /*蒙层样式“modal-backdrop”,他默认情况下是全屏黑色的:*/
  109. /*bootstrap.css文件第5424行~第5432行*/
  110. .modal-backdrop {
  111. position: fixed;
  112. top: 0;
  113. right: 0;
  114. bottom: 0;
  115. left: 0;
  116. z-index: 1040;
  117. background-color: #000;
  118. }
  119.  
  120. /*添加了一个过渡动画,从fade到in,把opacity值从0变成了0.5:*/
  121. /*bootstrap.css文件第5433行~第5440行*/
  122. .modal-backdrop.fade {
  123. filter: alpha(opacity=0);
  124. opacity: 0;
  125. }
  126. .modal-backdrop.in {
  127. filter: alpha(opacity=50);
  128. opacity: .5;
  129. }
  130.  
  131. /*媒体查询:*/
  132. @media (min-width: 768px) {
  133. .modal-sm {
  134. width: 300px;
  135. }
  136. }
  137. @media (min-width: 992px) {
  138. .modal-lg {
  139. width: 900px;
  140. }
  141. }
下一节