当前位置:   article > 正文

点击按钮弹出模态框实现_怎么函数通过点击图片显示模态框

怎么函数通过点击图片显示模态框

点击按钮弹出模态框的实现:

html:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>模态框</title>
  6. <link rel="stylesheet" type="text/css" href="modalBox.css">
  7. </head>
  8. <body>
  9. <!-- 触发按钮 -->
  10. <button id="triggerBtn">模态框</button>
  11. <!-- 模态框 -->
  12. <div id="myModal" class="modal">
  13. <div class="modal-content">
  14. <div class="modal-header">
  15. <h2>头部</h2>
  16. <span id="closeBtn" class="close">×</span>
  17. </div>
  18. <div class="modal-body">
  19. <p>这是一个模态框!</p>
  20. <p>喜欢就点个赞吧!</p>
  21. </div>
  22. <div class="modal-footer">
  23. <h3>尾部</h3>
  24. </div>
  25. </div>
  26. </div>
  27. <script type="text/javascript" src="modalBox.js"></script>
  28. </body>
  29. </html>
js:

  1. (function() {
  2. /*建立模态框对象*/
  3. var modalBox = {};
  4. /*获取模态框*/
  5. modalBox.modal = document.getElementById("myModal");
  6. /*获得trigger按钮*/
  7. modalBox.triggerBtn = document.getElementById("triggerBtn");
  8. /*获得关闭按钮*/
  9. modalBox.closeBtn = document.getElementById("closeBtn");
  10. /*模态框显示*/
  11. modalBox.show = function() {
  12. console.log(this.modal);
  13. this.modal.style.display = "block";
  14. }
  15. /*模态框关闭*/
  16. modalBox.close = function() {
  17. this.modal.style.display = "none";
  18. }
  19. /*当用户点击模态框内容之外的区域,模态框也会关闭*/
  20. modalBox.outsideClick = function() {
  21. var modal = this.modal;
  22. window.onclick = function(event) {
  23. if(event.target == modal) {
  24. modal.style.display = "none";
  25. }
  26. }
  27. }
  28. /*模态框初始化*/
  29. modalBox.init = function() {
  30. var that = this;
  31. this.triggerBtn.onclick = function() {
  32. that.show();
  33. }
  34. this.closeBtn.onclick = function() {
  35. that.close();
  36. }
  37. this.outsideClick();
  38. }
  39. modalBox.init();
  40. })();
css:

  1. /*模态框*/
  2. .modal {
  3. display: none; /* 默认隐藏 */
  4. position: fixed; /* 根据浏览器定位 */
  5. z-index: 1; /* 放在顶部 */
  6. left: 0;
  7. top: 0;
  8. width: 100%; /* 全宽 */
  9. height: 100%; /* 全高 */
  10. overflow: auto; /* 允许滚动 */
  11. background-color: rgba(0,0,0,0.4); /* 背景色 */
  12. }
  13. /*模态框内容*/
  14. .modal-content {
  15. display: flex; /*采用flexbox布局*/
  16. flex-direction: column; /*垂直排列*/
  17. position: relative;
  18. background-color: #fefefe;
  19. margin: 15% auto; /*距顶部15% 水平居中*/
  20. padding: 20px;
  21. border: 1px solid #888;
  22. width: 80%;
  23. animation: topDown 0.4s; /*自定义动画,从模态框内容上到下出现*/
  24. }
  25. @keyframes topDown {
  26. from {top: -300px; opacity: 0}
  27. to {top: 0; opacity: 1}
  28. }
  29. /*模态框头部*/
  30. .modal-header {
  31. display: flex; /*采用flexbox布局*/
  32. flex-direction: row; /*水平布局*/
  33. align-items: center; /*内容垂直居中*/
  34. justify-content: space-between;
  35. }
  36. /*关闭X 样式*/
  37. .close {
  38. color: #aaa;
  39. float: right;
  40. font-size: 28px;
  41. font-weight: bold;
  42. }
  43. .close:hover {
  44. color: black;
  45. text-decoration: none;
  46. cursor: pointer;
  47. }







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/117536?site
推荐阅读
相关标签
  

闽ICP备14008679号