Blogger Information
Blog 7
fans 0
comment 0
visits 5460
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简写一个模态框
Original
549 people have browsed it

一、html代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  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.0">
  7. <title>用户登陆</title>
  8. <!-- <style>
  9. @import url(./static/demo3.css);
  10. </style> -->
  11. <link rel="stylesheet" href="./static/demo3.css">
  12. </head>
  13. <body>
  14. <header>
  15. <h2 class="title">可可西里</h2>
  16. <button onclick="showLoginview()">登陆</button>
  17. </header>
  18. <!-- 模态框 -->
  19. <div class="model">
  20. <!-- 1、半透明背景 -->
  21. <div class="model-bg" onclick="closeModel()"></div>
  22. <!-- 2、登陆表单 -->
  23. <form action="" class="model-form">
  24. <fieldset>
  25. <legend>用户登陆</legend>
  26. <input type="email" placeholder="user@email.com">
  27. <input type="password" placeholder="不少于6位">
  28. <button>登陆</button>
  29. </fieldset>
  30. </form>
  31. <script src="./static/demo3.js"></script>
  32. </div>
  33. </body>
  34. </html>

二、css代码

  1. /* 初始化 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. /* 盒模型的计算方式:边距,边框全部计算在盒模型内 */
  6. font-size: border-box;
  7. }
  8. /* 头部样式 */
  9. header {
  10. background: rgb(80, 175, 108);
  11. padding: 0.5em 1em;
  12. display: flex;
  13. }
  14. /* 头部标题 */
  15. header .title {
  16. font-weight: 300;
  17. /* normal - 文字正常显示、italic - 文本以斜体显示 */
  18. font-style: italic;
  19. color: #fff;
  20. /* 字间距 */
  21. letter-spacing: 0.2rem;
  22. /* 字体阴影 :向右,向下,模糊半径,颜色*/
  23. text-shadow: 1px 2px 3px rgb(207 39 47);
  24. }
  25. /* 头部登陆按钮 */
  26. header button {
  27. /* auto 浏览器自动计算距离 */
  28. /* margin-left: auto;
  29. 居中:margin:0 auto; 或者: margin-left:auto; margin-right:auto;
  30. */
  31. margin: 0 0.5rem 0 auto;
  32. width: 5em;
  33. /* 无边框 :去除边框样式*/
  34. border: none;
  35. border-radius: 0.5em;
  36. }
  37. /* 登陆按钮点击特效 */
  38. header button:hover {
  39. /* 鼠标状态样式:手 */
  40. cursor: pointer;
  41. width: 5.1em;
  42. background-color: pink;
  43. color: #fff;
  44. /* 盒子加阴影:向右,向下,模糊半径,颜色 */
  45. box-shadow: 0 0 8px rgb(217, 238, 25);
  46. /* 加载过度动画 */
  47. transition: 0.3s;
  48. }
  49. /* 模态框 */
  50. .model .model-form fieldset {
  51. display: grid;
  52. gap: 1em;
  53. height: 15.5em;
  54. background-color: lightcyan;
  55. color: #fff;
  56. border: none;
  57. padding: 2em 3em;
  58. box-shadow: 0 0 8px #888;
  59. }
  60. /* 模态框表单标题 */
  61. .model .model-form fieldset legend {
  62. padding: 7px 1.5em;
  63. background-color: rgb(80, 175, 108);
  64. color: #fff;
  65. font-weight: 400;
  66. /* 字体间距:letter-spacing */
  67. letter-spacing: 2px;
  68. }
  69. /* 模态框-输入框样式 */
  70. .model .model-form fieldset input {
  71. height: 3em;
  72. padding-left: 1em;
  73. /* 去掉轮廓线 */
  74. outline: none;
  75. border: 1px solid #ddd;
  76. border-radius: 0.5em;
  77. font-size: 14px;
  78. }
  79. /* 模态框-输入框聚焦样式 */
  80. .model .model-form fieldset input:focus {
  81. border-color: #f0f0f0;
  82. box-shadow: 0 0 5px yellow;
  83. }
  84. /* 模态框-登陆按钮样式 */
  85. .model .model-form fieldset button {
  86. background-color: lightseagreen;
  87. border: none;
  88. border-radius: 0.5em;
  89. font-size: 16px;
  90. height: 2.5em;
  91. color: #fff;
  92. }
  93. /* 模态框-登陆按钮点击样式 */
  94. .model .model-form fieldset button:hover {
  95. background-color: rgb(76, 204, 64);
  96. /* 鼠标样式:手 */
  97. cursor: pointer;
  98. box-shadow: 0 0 5px yellow;
  99. /* 加载过度时间 */
  100. transition: 0.3s;
  101. }
  102. /* 定位-模态框定位 */
  103. .model .model-form {
  104. position: fixed;
  105. top: 10em;
  106. left: 38em;
  107. right: 38em;
  108. }
  109. /* 定位-遮罩定位 */
  110. .model .model-bg {
  111. position: fixed;
  112. /* 坐标全部清零,定位到四个顶点 */
  113. top: 0;
  114. left: 0;
  115. right: 0;
  116. bottom: 0;
  117. background-color: rgb(0, 0, 0, 0.5);
  118. }
  119. /* 模态框-登陆页面初始化隐藏 */
  120. .model {
  121. display: none;
  122. }

三、js代码

  1. function showLoginview() {
  2. // 获取模态框class元素
  3. const mod = document.querySelector('.model');
  4. // 显示模态框登陆页面
  5. mod.style.display = 'block';
  6. // 自动聚焦第一个输入框
  7. mod.querySelector('input:first-of-type').focus();
  8. }
  9. //遮罩任意处关闭模态框
  10. function closeModel() {
  11. // 获取模态框元素
  12. const mod = document.querySelector('.model');
  13. // 关闭模态框
  14. mod.style.display = 'none';
  15. }

演示:
00

01

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post