模态框

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. }

呈现效果:

更多相关文章

  1. android 基于百度地图api开发定位以及获取详细地址
  2. Android代码混淆之混淆规则
  3. android播放rtsp文件
  4. Android两行代码真正杀死你的App
  5. Android(安卓)HAL 开发 (1)
  6. 如何在自己的程序中添加appWidget(附简单代码)
  7. Android(安卓)高手进阶教程(十三)之----Android(安卓)数据库SQLi
  8. php自动加载代码实例详解
  9. 将模态框案例再动手写一遍,不要全抄课堂源码,要有自己的创意

随机推荐

  1. golang 如何并发
  2. golang如何调试
  3. golang 如何实现微服务
  4. golang 哪些功能用标准库
  5. golang协程如何关闭
  6. golang判断今天星期几
  7. golang 可以多继承吗
  8. golang的编译器是什么
  9. golang 如何判断文件是否存在
  10. golang 管道线程安全吗