Blogger Information
Blog 12
fans 0
comment 0
visits 5454
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
HTML 模态框学习
。。。
Original
640 people have browsed it

HTML 学习

1.模态框

  • 例子
    效果演示1

  • 语法
    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. }

css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. html {
  6. height: 100%;
  7. }
  8. body {
  9. min-height: 100%;
  10. margin: 0;
  11. padding: 0;
  12. position: relative;
  13. }
  14. header {
  15. background-color: lightblue;
  16. padding: 0.5em 1em;
  17. display: flex;
  18. }
  19. main {
  20. background: #ccc;
  21. text-align: center;
  22. }
  23. footer {
  24. position: absolute;
  25. color: #fff;
  26. bottom: 0;
  27. width: 100%;
  28. height: 100px;
  29. line-height: 100px;
  30. text-align: center;
  31. background-color: #000;
  32. }
  33. /* logo */
  34. header .title {
  35. font-weight: lighter;
  36. font-style: italic;
  37. color: white;
  38. letter-spacing: 5px;
  39. text-shadow: 2px 2px 5px red;
  40. font-size: 2em;
  41. }
  42. /* 登录按钮 */
  43. header button {
  44. margin-left: auto;
  45. width: 10em;
  46. border: none;
  47. padding: 0.5em 1em;
  48. border-radius: 0.5em;
  49. box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  50. }
  51. header button:hover {
  52. cursor: pointer;
  53. background-color: lawngreen;
  54. transition: 0.5s;
  55. box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
  56. }
  57. /* 定位 */
  58. .modal .modal-form {
  59. position: fixed;
  60. top: 10em;
  61. left: 40%;
  62. }
  63. .modal .modal-form fieldset {
  64. height: 15.5em;
  65. background-color: lightcyan;
  66. border: none;
  67. padding: 2em 3em;
  68. box-shadow: 0 0 5px #888;
  69. }
  70. .modal .modal-form fieldset legend {
  71. width: 5em;
  72. text-shadow: 0 0 3px lightgreen, 0 0 5px lightcoral;
  73. }
  74. .modal .modal-form fieldset input:focus {
  75. box-shadow: 0 0 8px #888;
  76. border: none;
  77. }
  78. .modal .modal-form fieldset input {
  79. height: 2em;
  80. padding-left: 1em;
  81. outline: none;
  82. border: 1px solid #ddd;
  83. font-size: 14px;
  84. }
  85. .modal .modal-bg {
  86. position: fixed;
  87. /* 坐标全部清0,请定位到四个顶点 */
  88. top: 0;
  89. left: 0;
  90. right: 0;
  91. bottom: 0;
  92. background-color: rgb(0, 0, 0, 0.5);
  93. }
  94. .modal {
  95. display: none;
  96. }
  97. button {
  98. -webkit-transition-duration: 0.4s;
  99. transition-duration: 0.4s;
  100. }
  101. button:hover {
  102. background-color: #4caf50;
  103. color: white;
  104. }

HTML

  1. <header>
  2. <h2 class="title">我的学习空间</h2>
  3. <button onclick="showModal()">登录/注册</button>
  4. </header>
  5. <main></main>
  6. <footer>
  7. 学习标语:梯子的梯阶从来不是用来搁脚的,它只是让人们的脚放上一段时间,以便让别一只脚能够再往上登。
  8. </footer>
  9. <!-- 模态框 -->
  10. <div class="modal">
  11. <!-- 1. 半透明的遮罩 -->
  12. <div class="modal-bg" onclick="closeModal()"></div>
  13. <!-- 2. 弹层表单 -->
  14. <form action="" class="modal-form">
  15. <fieldset style="display: grid; gap: 1em">
  16. <legend>用户登录</legend>
  17. <div>
  18. <label for="email">用户名:</label>
  19. <input
  20. type="email"
  21. name="email"
  22. id="my-email"
  23. placeholder="user@email.com"
  24. />
  25. </div>
  26. <div>
  27. <label for="password">密&nbsp; &nbsp; 码:</label>
  28. <input
  29. type="password"
  30. name="password"
  31. id="my-password"
  32. placeholder="不少于6位"
  33. />
  34. </div>
  35. <button>登录</button>
  36. <button>注册</button>
  37. </fieldset>
  38. </form>
  39. </div>
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