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

模态框案例

效果演示:


html+js代码如下:

  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="css/modal-box.css" />
  9. </head>
  10. <body>
  11. <!-- 页面头部 -->
  12. <header>
  13. <h2 class="title">php中文网-程序员开始的地方!</h2>
  14. <button onclick="showModal()">登录</button>
  15. </header>
  16. <!-- 模态框 -->
  17. <div class="modal">
  18. <!-- 1. 半透明的遮罩 -->
  19. <div class="modal-bg" onclick="closeModal()"></div>
  20. <!-- 2. 弹层表单 -->
  21. <form action="" class="modal-form">
  22. <fieldset style="display: grid; gap: 1em">
  23. <legend>会员登陆</legend>
  24. <input type="email" name="email" placeholder="请输入手机或邮箱账号" />
  25. <input type="password" name="password" placeholder="请输入密码" />
  26. <button>登录</button>
  27. </fieldset>
  28. </form>
  29. </div>
  30. </body>
  31. </html>
  32. <script>
  33. //js代码开始
  34. function showModal() {
  35. // 获取模态框元素
  36. const modal = document.querySelector(".modal");
  37. // 显示模态框
  38. modal.style.display = "block";
  39. // 焦点自动置入第一个输入框email
  40. modal.querySelector("input:first-of-type").focus();
  41. }
  42. function closeModal() {
  43. // 获取模态框元素
  44. const modal = document.querySelector(".modal");
  45. // 关闭模态框
  46. modal.style.display = "none";
  47. }
  48. //js代码结束
  49. </script>

css代码:

  1. /* 初始化 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 头部 */
  8. header {
  9. background-color: lightseagreen;
  10. padding: 0.5em 1em;
  11. display: flex;
  12. }
  13. /* logo */
  14. header .title {
  15. font-weight: lighter;
  16. font-style: italic;
  17. color: white;
  18. letter-spacing: 2px;
  19. text-shadow: 1px 1px 1px #555;
  20. }
  21. /* 登录按钮 */
  22. header button {
  23. margin-left: auto;
  24. width: 5em;
  25. border: none;
  26. border-radius: 0.5em;
  27. }
  28. header button:hover {
  29. cursor: pointer;
  30. background-color: coral;
  31. color: #fff;
  32. box-shadow: 0 0 5px #fff;
  33. transition: 0.3s;
  34. }
  35. /* 模态框 */
  36. .modal .modal-form fieldset {
  37. height: 15.5em;
  38. background-color: lightcyan;
  39. border: none;
  40. padding: 2em 3em;
  41. box-shadow: 0 0 5px #888;
  42. }
  43. /* 模态框表单标题 */
  44. .modal .modal-form fieldset legend {
  45. padding: 7px 1.5em;
  46. background-color: lightseagreen;
  47. color: white;
  48. }
  49. .modal .modal-form fieldset input {
  50. height: 3em;
  51. padding-left: 1em;
  52. outline: none;
  53. border: 1px solid #ddd;
  54. font-size: 14px;
  55. }
  56. /* :focus: 表单控件,获取焦点时的样式 */
  57. .modal .modal-form fieldset input:focus {
  58. box-shadow: 0 0 8px #888;
  59. border: none;
  60. }
  61. .modal .modal-form fieldset button {
  62. background-color: lightseagreen;
  63. color: white;
  64. border: none;
  65. height: 3em;
  66. font-size: 16px;
  67. height: 2.5em;
  68. }
  69. .modal .modal-form fieldset button:hover {
  70. background-color: coral;
  71. cursor: pointer;
  72. }
  73. /* 定位 */
  74. .modal .modal-form {
  75. position: fixed;
  76. top: 10em;
  77. left: 38em;
  78. right: 38em;
  79. }
  80. /* 遮罩 */
  81. .modal .modal-bg {
  82. position: fixed;
  83. /* 坐标全部清0,请定位到四个顶点 */
  84. top: 0;
  85. left: 0;
  86. right: 0;
  87. bottom: 0;
  88. background-color: rgb(0, 0, 0, 0.5);
  89. }
  90. .modal {
  91. display: none;
  92. }
批改老师: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+教程免费学