博客列表 >css模态框学习

css模态框学习

阿杰
阿杰原创
2022年03月25日 10:58:59672浏览

一、模态框

  • html部分
  1. <!-- 头部 -->
  2. <header>
  3. <h3 class="title">xxx的博客欢迎你</h3>
  4. <button onclick="showModal()">登录</button>
  5. </header>
  6. <!-- 模态框弹层 -->
  7. <div class="modal">
  8. <!-- 遮罩层 -->
  9. <div class="modal-bg" onclick="closeModal()"></div>
  10. <div class="modal-content">
  11. <!-- 登录表单 -->
  12. <form action="" class="modal-form" id="content1">
  13. <fieldset>
  14. <legend>用户登录</legend>
  15. <input type="email" name="emain" id="" placeholder="请输入邮箱">
  16. <input type="password" name="password" id="" placeholder="请输入密码">
  17. <button>登录</button>
  18. </fieldset>
  19. </form>
  20. <!-- 登录二维码 -->
  21. <img id="content2" class="ewmImg" src="./ewm.jpg" alt="登录二维码">
  22. <div class="loginItem">
  23. <div><a href="#content1">账号登录</a></div>
  24. <div><a href="#content2">扫码登录</a></div>
  25. </div>
  26. </div>
  27. </div>
  • css部分
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. header{
  6. background-color: darkcyan;
  7. padding: 1em;
  8. display: flex;
  9. place-content: space-between;
  10. line-height: 4vh;
  11. }
  12. .title{
  13. color: #fff;
  14. font-weight: lighter;
  15. font-style: italic;
  16. letter-spacing: 2px;
  17. text-shadow: 2px 2px 2px #555;
  18. }
  19. header button{
  20. border: none;
  21. width: 5em;
  22. border-radius: 8px;
  23. }
  24. header button:hover{
  25. cursor: pointer;
  26. background-color: darkorange;
  27. box-shadow: 0 0 5px #fff;
  28. transition: 0.2s;
  29. color: #fff;
  30. }
  31. .modal{
  32. display: none;
  33. }
  34. .modal-bg{
  35. position: absolute;
  36. left: 0;
  37. top: 0;
  38. width: 100%;
  39. height: 100%;
  40. background-color: rgba(0, 0, 0, .5);
  41. }
  42. .modal-content{
  43. position: fixed;
  44. width: 300px;
  45. top: 25vh;
  46. left: 35vw;
  47. }
  48. .modal .modal-form fieldset {
  49. height: 15.5em;
  50. background-color: lightcyan;
  51. border: none;
  52. padding: 2em 3em;
  53. box-shadow: 0 0 5px #888;
  54. display: grid;
  55. gap: 1em;
  56. }
  57. /* 模态框表单标题 */
  58. .modal-form{
  59. margin-top: 6vh;
  60. }
  61. .modal .modal-form fieldset legend {
  62. padding: 7px 1.5em;
  63. background-color: lightseagreen;
  64. color: white;
  65. }
  66. .modal .modal-form fieldset input {
  67. height: 3em;
  68. padding-left: 1em;
  69. outline: none;
  70. border: 1px solid #ddd;
  71. font-size: 14px;
  72. }
  73. /* :focus: 表单控件,获取焦点时的样式 */
  74. .modal .modal-form fieldset input:focus {
  75. box-shadow: 0 0 8px #888;
  76. border: none;
  77. }
  78. .modal .modal-form fieldset button {
  79. background-color: lightseagreen;
  80. color: white;
  81. border: none;
  82. height: 3em;
  83. font-size: 16px;
  84. height: 2.5em;
  85. }
  86. .modal .modal-form fieldset button:hover {
  87. background-color: coral;
  88. cursor: pointer;
  89. }
  90. .modal-content{
  91. padding: 30px;
  92. background-color: #fff;
  93. }
  94. .loginItem{
  95. display: flex;
  96. place-content: space-evenly;
  97. margin-bottom: 2vh;
  98. position:absolute ;
  99. width:90%;
  100. top: 2vh;
  101. }
  102. .loginItem div{
  103. color: #999;
  104. line-height: 5vh;
  105. }
  106. .active{
  107. border-bottom: 2px solid lightseagreen;
  108. color: lightseagreen!important;
  109. }
  110. .ewmImg{
  111. width: 100%;
  112. margin-top: 6vh;
  113. }
  114. a{
  115. text-decoration: none;
  116. color: #999;
  117. }
  118. #content1,#content2{
  119. display:none;
  120. }
  121. #content1:target,
  122. #content2:target{
  123. display:block;
  124. }
  125. #content1:target ~ .loginItem div:first-of-type {
  126. border-bottom: 2px solid lightseagreen;
  127. }
  128. #content1:target ~ .loginItem div:first-of-type a{
  129. color: lightseagreen;
  130. }
  131. #content2:target ~ .loginItem div:nth-of-type(2) a{
  132. color: lightseagreen;
  133. }
  134. #content1:target ~ #content2{
  135. display: none;
  136. }
  137. #content2:target ~ .loginItem div:nth-of-type(2){
  138. border-bottom: 2px solid lightseagreen;
  139. }
  • 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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议