博客列表 >模仿模态框

模仿模态框

新手1314
新手1314原创
2022年03月24日 09:57:58465浏览

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="zuoye.css" />
  9. </head>
  10. <body>
  11. <header>
  12. <h1 class="title">新手1314的博客</h1>
  13. <button onclick="show()">登录</button>
  14. </header>
  15. <div class="show">
  16. <div class="show-ad" onclick="close()"></div>
  17. <form action="" class="show-form">
  18. <fieldset style="display: grid; gap: 1em">
  19. <legend>用户登录</legend>
  20. <input type="text" name="user" placeholder="请输入用户名" />
  21. <input type="password" name="password" placeholder="请输入密码" />
  22. <button>登录</button>
  23. </fieldset>
  24. </form>
  25. </div>
  26. <script src="zuoye.js"></script>
  27. </body>
  28. </html>

css样式:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. header {
  7. background-color: aqua;
  8. padding: 0.5em 1em;
  9. display: flex;
  10. }
  11. header .title {
  12. color: #fff;
  13. font-weight: lighter;
  14. letter-spacing: 3px;
  15. text-shadow: 1px 1px 1px #000;
  16. }
  17. header button {
  18. margin-left: auto;
  19. border: none;
  20. border-radius: 0.5em;
  21. width: 60px;
  22. }
  23. header button:hover {
  24. cursor: pointer;
  25. background-color: black;
  26. color: beige;
  27. transform: 0.5s;
  28. box-shadow: 0 0 5px #fff;
  29. }
  30. .show .show-form fieldset {
  31. background-color: blanchedalmond;
  32. border: 0;
  33. height: 16em;
  34. padding: 2em 3em;
  35. box-sizing: 0 0 5px #000;
  36. }
  37. .show .show-form fieldset legend {
  38. background-color: violet;
  39. color: #000;
  40. padding: 7px 1.5em;
  41. }
  42. .show .show-form fieldset input {
  43. font-size: 14px;
  44. padding-left: 1em;
  45. outline: none;
  46. border: 2px solid #fff;
  47. height: 3em;
  48. }
  49. .show .show-form fieldset input:focus {
  50. border: 0;
  51. box-shadow: 0 0 8px #888;
  52. }
  53. .show .show-form fieldset button {
  54. background-color: aqua;
  55. color: #000;
  56. border: 0;
  57. font-size: 16px;
  58. height: 2.5em;
  59. }
  60. .show .show-form fieldset button:hover {
  61. cursor: pointer;
  62. background-color: aquamarine;
  63. }
  64. .show .show-form {
  65. position: fixed;
  66. left: 50em;
  67. right: 50em;
  68. top: 10em;
  69. }
  70. .show .show-ad {
  71. position: fixed;
  72. top: 0;
  73. left: 0;
  74. right: 0;
  75. bottom: 0;
  76. background-color: rgb(0, 0, 0, 0.8);
  77. }
  78. .show {
  79. display: none;
  80. }

js代码:

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

实现:

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