博客列表 >模态框基础应用

模态框基础应用

想做肥仔
想做肥仔原创
2022年03月27日 19:05:05607浏览

模态框基础应用

HTML部分

  1. <header>
  2. <h2 class="title">QQ空间</h2>
  3. <!-- onclick="showModal()" 点击时触发事件 -->
  4. <button onclick="showModal()">登陆</button>
  5. </header>
  6. <div class="box_img">
  7. <img src="20171123181522_c48800.jpg" alt="">
  8. </div>
  9. <!-- 模态框 -->
  10. <div class="modal_box">
  11. <!-- 背景遮罩 -->
  12. <div class="shade" onclick="closeModal()"></div>
  13. <!-- 登陆表单 -->
  14. <form action="" class="modal_form">
  15. <fieldset style="display: grid; gap: 1em">
  16. <legend>登陆QQ</legend>
  17. <!-- <label for="">QQ账号</label> -->
  18. <input type="number" name="number" placeholder="输入数字">
  19. <!-- <label for="">QQ密码</label> -->
  20. <input type="password" name="password" placeholder="">
  21. <button>登陆</button>
  22. </fieldset>
  23. </form>
  24. </div>
  25. <script src="modal.js"></script>

css部分代码

  1. /* 初始化 box-sizing:border-box 让盒子边框和内容区重叠 */
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 登录头部 */
  8. header{
  9. padding: 0.5em 1em;
  10. background-color: whitesmoke;
  11. display: flex;
  12. }
  13. .box_img{
  14. width: auto;
  15. height: auto;
  16. }
  17. .box_img img{
  18. width: 100%;
  19. height: 1000px;
  20. position: fixed;
  21. }
  22. /* 字体logo设置 font-weight 字体粗细设置 letter-spacing字体间距设置 text-shadow字体阴影设置*/
  23. header .title{
  24. font-weight: 100;
  25. font-style: italic;
  26. color: black;
  27. letter-spacing: 2px;
  28. text-shadow: 1px 1px 1px #555;
  29. }
  30. /* 登录按钮 border: none 去掉边框 设置边框角度border-radius: 0.5em */
  31. header button{
  32. margin-left: auto;
  33. width: 10em;
  34. border: none;
  35. background-color: #86ce2f;
  36. border-radius: 0.5em;
  37. }
  38. /* :hover鼠标移动 触发, 一个鼠标手cursor: pointer*/
  39. header button:hover{
  40. cursor: pointer;
  41. background-color: green;
  42. color: white;
  43. box-sizing: 0 0 0.5px white;
  44. transition: 0.4s;
  45. }
  46. /* 模态框 设置阴影box-shadow: 0 0 5px #888 */
  47. .modal_box .modal_form fieldset {
  48. height: 15em;
  49. background-color: white;
  50. border: none;
  51. padding: 3em 4em;
  52. box-shadow: 0 0 5px #888;
  53. }
  54. /* 标题 */
  55. .modal_box .modal_form fieldset legend{
  56. padding:7px 2em ;
  57. color: wheat;
  58. background-color: #86ce2f;
  59. border-radius: 0.5em;
  60. }
  61. /* input输入框设置 */
  62. .modal_box .modal_form fieldset input {
  63. height: 3em;
  64. padding-left: 1em;
  65. outline: none;
  66. border: 1px solid #ddd;
  67. font-size: 14px;
  68. border-radius: 0.5em;
  69. }
  70. /* 获取焦点设置focus 添加边框阴影去掉边框 */
  71. .modal_box .modal_form fieldset input:focus {
  72. box-shadow: 0 0 8px #888;
  73. border: none;
  74. }
  75. /* 输入框按钮设置 */
  76. .modal_box .modal_form fieldset button{
  77. background-color: #86ce2f;
  78. color: white;
  79. border: none;
  80. height: 3em;
  81. font-size: 16px;
  82. height: 2.5em;
  83. border-radius: 0.5em;
  84. }
  85. /* hover获取焦点触发事件 */
  86. .modal_box .modal_form fieldset button:hover{
  87. background-color: green;
  88. cursor: pointer;
  89. transition: 0.4s;
  90. border-radius: 0.5em;
  91. }
  92. /* 模态框定位 */
  93. .modal_box .modal_form{
  94. position: fixed;
  95. top: 10em;
  96. left: 38em;
  97. right: 38em;
  98. }
  99. .modal_box .shade{
  100. position: fixed;
  101. top: 0;
  102. left: 0;
  103. right: 0;
  104. bottom: 0;
  105. background-color: rgb(0, 0, 0, 0.5);
  106. }
  107. /* 隐藏 */
  108. .modal_box {
  109. display: none;
  110. }

js部分代码

  1. function showModal() {
  2. // 获取模态框元素
  3. const modal = document.querySelector('.modal_box');
  4. // 显示模态框
  5. modal.style.display = 'block';
  6. // 焦点自动置入第一个输入框email
  7. modal.querySelector('input:first-of-type').focus();
  8. }
  9. function closeModal() {
  10. // 获取模态框元素
  11. const modal = document.querySelector('.modal_box');
  12. // 关闭模态框
  13. modal.style.display = 'none';
  14. }

示例图

1

示例1

2

示例2

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议