博主信息
博文 28
粉丝 0
评论 1
访问量 18451
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
模态框案例
centos
原创
665人浏览过

模态框案例

绝对定位的实例演示,将常见登录界面实现

以下是代码和相关注释
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="0323.css" />
  9. </head>
  10. <body>
  11. <header>
  12. <h2 class="title">张峰的博客</h2>
  13. <button onclick="showModal()">登录</button>
  14. </header>
  15. <!-- 模态框 -->
  16. <div class="model">
  17. <!-- 1.遮罩 -->
  18. <div class="model-bg"></div>
  19. <!-- 2.登录表单 -->
  20. <!-- 2.1 表单分组 -->
  21. <form action="" class="model-form">
  22. <fieldset style="display: grid; gap: 1em">
  23. <!-- 2.2表头 -->
  24. <legend>系统后台登录</legend>
  25. 用户名:<input type="text" /> 用户密码:<input type="password" />
  26. <button>登录</button>
  27. <p>登录账号请联系管理获取</p>
  28. </fieldset>
  29. </form>
  30. </div>
  31. <script src="modal.js"></script>
  32. </body>
  33. </html>

css

  1. /* 初始化 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 头部 */
  8. header {
  9. display: flex;
  10. background-color: hsl(46, 100%, 50%);
  11. padding: 0.5em 1em;
  12. }
  13. /* 登录按钮 */
  14. header button {
  15. margin-left: auto;
  16. border: none;
  17. width: 5em;
  18. border-radius: 0.5em;
  19. }
  20. header button:hover {
  21. background-color: rgb(240, 166, 178);
  22. color: white;
  23. /* 鼠标变为小手 */
  24. cursor: pointer;
  25. /* 延时变化 */
  26. transition: 0.5s;
  27. box-shadow: 0 0 5px #fff;
  28. }
  29. /* logo */
  30. header .title {
  31. font-weight: lighter;
  32. font-style: italic;
  33. color: white;
  34. /* 字符间距 */
  35. letter-spacing: 2px;
  36. /* 字体阴影 */
  37. text-shadow: 1px 1px 1px #555;
  38. }
  39. /* 模态框 */
  40. .model .model-form fieldset {
  41. height: 25em;
  42. background-color: lightcyan;
  43. border: none;
  44. padding: 2em 3em;
  45. box-shadow: 0 0 5px #888;
  46. }
  47. /* 模态框表单标题 */
  48. .model .model-form fieldset legend {
  49. padding: 7px 1.5em;
  50. background-color: lightseagreen;
  51. color: white;
  52. margin-left: auto;
  53. margin-right: auto;
  54. }
  55. .model .model-form fieldset input {
  56. height: 3em;
  57. padding-left: 1em;
  58. outline: none;
  59. border: 1px solid #ddd;
  60. font-size: 14px;
  61. }
  62. /* :focus: 表单控件,获取焦点时的样式 */
  63. .model .model-form fieldset input:focus {
  64. box-shadow: 0 0 8px #888;
  65. border: none;
  66. }
  67. .model .model-form fieldset button {
  68. background-color: lightseagreen;
  69. color: white;
  70. border: none;
  71. height: 3em;
  72. font-size: 16px;
  73. height: 2.5em;
  74. width: 5em;
  75. margin-left: auto;
  76. margin-right: auto;
  77. }
  78. .model .model-form fieldset button:hover {
  79. background-color: coral;
  80. cursor: pointer;
  81. }
  82. p {
  83. margin-left: auto;
  84. margin-right: auto;
  85. }
  86. /* 定位 */
  87. .model .model-form {
  88. position: fixed;
  89. top: 10em;
  90. left: 38em;
  91. right: 38em;
  92. }
  93. /* 遮罩定位 */
  94. .model-bg {
  95. position: fixed;
  96. top: 0;
  97. left: 0;
  98. right: 0;
  99. bottom: 0;
  100. background-color: rgb(0, 0, 0, 0.5);
  101. }
  102. .model {
  103. display: none;
  104. }

js

  1. function showModal() {
  2. // 获取模态框元素
  3. const modal = document.querySelector(".model");
  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(".model");
  12. // 关闭模态框
  13. modal.style.display = "none";
  14. }
  • 登录初始样子
  • 点击登录后的样子
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学