博客列表 >模态框学习与实践

模态框学习与实践

P粉317509817
P粉317509817原创
2022年03月24日 11:30:08473浏览

模态框

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. <link rel="stylesheet" href="模态框.css">
  9. <script src="模态框.js"></script>
  10. </head>
  11. <body>
  12. <header>
  13. <h2 class="title">二刺螈</h2>
  14. <figure>
  15. <div>
  16. <span>Hover Me</span>
  17. <span onclick="showModal()">登录</span>
  18. </div>
  19. </figure>
  20. </header>
  21. <div class="jpg">
  22. <!-- 内容区 -->
  23. </div>
  24. <!-- 模态框 -->
  25. <div class="modal">
  26. <!-- 半透明遮罩 -->
  27. <div class="modal-bg" onclick="closeModal()"></div>
  28. <!-- 弹层表单 -->
  29. <form action="" class="modal-form">
  30. <fieldset style="display: grid; gap: 1em;">
  31. <legend>用户登录</legend>
  32. <input type="email" name="emali" placeholder="user@mail.com"/>
  33. <input type="password" name="psw" id="psw" placeholder="不少于六位密码">
  34. <button>登录</button>
  35. </fieldset>
  36. </form>
  37. </div>
  38. </body>
  39. </html>

css样式:

  1. /* 初始化 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 头部样式 */
  8. header {
  9. background-color: #2C3E50;
  10. padding: 0.5em 1em;
  11. display: flex;
  12. height: 10vh;
  13. }
  14. header .title {
  15. font-weight:400;
  16. font-style:normal;
  17. font-size: 35pt;
  18. color: aliceblue;
  19. letter-spacing: 10px;
  20. text-shadow: 1px 1px 1px #D0D0D0;
  21. }
  22. /* text-shadow:(水平阴影(必须),垂直阴影(必须),模糊距离(可选),阴影颜色(必须)) */
  23. /* letter-spacing:(字间距) */
  24. @import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro");
  25. figure {
  26. width: 200px;
  27. height: 60px;
  28. cursor: pointer;
  29. perspective: 500px;
  30. -webkit-perspective: 500px;
  31. margin-left: auto;
  32. margin-top: 10px;
  33. }
  34. figure div {
  35. height: 100%;
  36. transform-style: preserve-3d;
  37. -webkit-transform-style: preserve-3d;
  38. transition: 0.25s;
  39. -webkit-transition: 0.25s;
  40. }
  41. figure:hover div {
  42. transform: rotateX(-90deg);
  43. }
  44. span {
  45. width: 100%;
  46. height: 100%;
  47. position: absolute;
  48. box-sizing: border-box;
  49. border: 5px solid #fff;
  50. font-family: "Source Sans Pro", sans-serif;
  51. line-height: 50px;
  52. font-size: 17pt;
  53. text-align: center;
  54. text-transform: uppercase;
  55. }
  56. span:nth-child(1) {
  57. color: #fff;
  58. transform: translate3d(0, 0, 30px);
  59. -webkit-transform: translate3d(0, 0, 30px);
  60. }
  61. span:nth-child(2) {
  62. color: #1c175d;
  63. background: #fff;
  64. transform: rotateX(90deg) translate3d(0, 0, 30px);
  65. -webkit-transform: rotateX(90deg) translate3d(0, 0, 30px);
  66. }
  67. /* 模态框 */
  68. .modal .modal-form{
  69. width: 20em;
  70. height: 20em;
  71. }
  72. .modal .modal-form fieldset{
  73. height: 15.5em;
  74. width: 20em;
  75. background-color: #d0d0d0;
  76. border:none;
  77. padding: 2rem,3em;
  78. box-shadow: 0 0 5px #888;
  79. border-radius: 0 0 1em 1em ;
  80. }
  81. /* 模态框表单标题 */
  82. .modal .modal-form fieldset legend{
  83. padding:5px 1.5em;
  84. height: 3em;
  85. width: 25em;
  86. background-color: #2C3E50;
  87. color: white;
  88. text-align: center;
  89. border-radius: 1em 1em 0 0;
  90. }
  91. /* 表单 */
  92. .modal .modal-form fieldset input{
  93. height: 3em;
  94. width: 25em;
  95. padding: 0.6em;
  96. outline: none;
  97. border:1px solid #2C3E50;
  98. font-size: 14px;
  99. margin: 0.6em auto;
  100. }
  101. .modal .modal-form fieldset button{
  102. width: 10em;
  103. text-align: center;
  104. position: relative;
  105. left: 7.5em;
  106. bottom: 0.6em;
  107. }
  108. /* focus */
  109. .modal .modal-form fieldset input :focus{
  110. box-shadow: 0 0 8px #888;
  111. border:none;
  112. }
  113. .modal .modal-form fieldset button{
  114. background-color: #2C3E50;
  115. color: white;
  116. border:none;
  117. font-size: 16px;
  118. height: 2.5em;
  119. }
  120. .modal .modal-form fieldset button:hover{
  121. cursor: pointer;
  122. background-color:black;
  123. transition: 0.3s;
  124. box-shadow: 4px 4px 1px white;
  125. }
  126. /* 定位 */
  127. .modal .modal-form{
  128. position:fixed;
  129. top:20em;
  130. left: auto;
  131. right: 50em;
  132. }
  133. .modal .modal-form fieldset legend {
  134. position: relative;
  135. bottom: 1.01em;
  136. }
  137. /* 遮罩 */
  138. .modal .modal-bg{
  139. position: fixed;
  140. top:0;
  141. left: 0;
  142. right: 0;
  143. bottom: 0;
  144. /* 定位在四个方向的原点 */
  145. background-color: rgb(0,0,0,0.5);
  146. }
  147. .modal {
  148. display: none;
  149. }
  150. .jpg {
  151. width: 100vw;
  152. height: 100vh;
  153. background-color: #D0D0D0;
  154. }

JS代码:

  1. function showModal() {
  2. // 获取模态框元素
  3. const modal = document.querySelector('.modal');
  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');
  12. // 关闭模态框
  13. modal.style.display = 'none';
  14. }

呈现效果:

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