模态框案例

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

以下是代码和相关注释
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. }
  • 登录初始样子
  • 点击登录后的样子

更多相关文章

  1. 用PDO连接mysql,用预处理语句实现用户登录时防sql注入小结
  2. android中的Sqlite数据库操作
  3. android传送照片到FTP服务器
  4. jsp form表单方法示例
  5. android 微信登录
  6. PHP如何防止用户重复提交表单
  7. 搭建EXTJS和STRUTS2框架(ext和struts2简单实例)
  8. 详解MacOs免密登录CentOs操作步骤
  9. Android(安卓)app的登录和注册功能

随机推荐

  1. TextView 文字加图片显示效果
  2. android 使控件透明
  3. Android中对NFC的实现代码分布在如下几个
  4. android定位布局
  5. android layout_weight了解
  6. Android修改自己程序字体的方法详解
  7. Android:解决RadioGroup中RadioButton的图
  8. Android 常用组件,的常用类型
  9. Android开发EditText属性
  10. Android之ActivityManager与Proxy模式的