博主信息
博文 70
粉丝 1
评论 0
访问量 68760
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
定位的类型-实现一个模态框
葡萄枝子
原创
888人浏览过

定位的类型-实现一个模态框

  1. 简述定位的类型与应用场景和使用条件
  2. 模仿课堂案例,实现一个模态框

1. 简述定位的类型与应用场景和使用条件

四种定位类型和属性

  • position:static 静态定位,默认属性,与文档流源码顺序一致
  • position:relative 相对定位,相对于文档流原始位置的偏移
  • position:absolute 绝对定位,相对祖先最近定位元素原始位置的偏移,若没有祖先定位元素,则相对于根 html 的偏移,绝对定位元素会脱离文档流
  • position:fixed 固定定位,绝对定位的一个特例,是相对于根 html 的偏移

元素中有 position:relativeposition:absolute 就是一个定位元素

转为定位元素 position:relative,它内部的元素就相对于它进行绝对定位

固定定位 position:absolute 可应用于 在线客服,广告位等

2. 模仿课堂案例,实现一个模态框

  • 模仿模态框
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>模仿课堂案例,实现一个模态框</title>
  7. <link rel="stylesheet" href="css/modal.css">
  8. </head>
  9. <body>
  10. <header>
  11. <h2>我的博客</h2><button>登录</button>
  12. </header>
  13. <div class="modal">
  14. <div class="modal-drop"></div>
  15. <div class="modal-body">
  16. <button class="close">关闭</button>
  17. <fieldset>
  18. <legend>登录框</legend>
  19. <form action="" method="post">
  20. <p>
  21. <label for="user-name">用户名</label>
  22. <input type="text" name="username" id="user-name">
  23. </p>
  24. <p>
  25. <label for="user-pass">密码</label>
  26. <input type="password" name="userpass" id="user-pass">
  27. </p>
  28. <p>
  29. <button>登录</button>
  30. </p>
  31. </form>
  32. </fieldset>
  33. </div>
  34. </div>
  35. <!-- 模态框js -->
  36. <script src="js/modal.js"></script>
  37. </body>
  38. </html>
  • 引入的 css/modal.css 为
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. /* 页眉 */
  7. header {
  8. background-color: #ccc;
  9. padding: 0.5em 1em;
  10. /* BFC 清除浮动 */
  11. overflow: hidden;
  12. }
  13. header h2 {
  14. /* 左浮动 */
  15. float: left;
  16. }
  17. header button {
  18. /* 右浮动 */
  19. float: right;
  20. width: 5em;
  21. height: 2.5em;
  22. }
  23. header button:hover{
  24. cursor: pointer;
  25. }
  26. /* 模态框初始化隐藏 */
  27. .modal {
  28. display: none;
  29. }
  30. /* 遮罩层 */
  31. .modal .modal-drop {
  32. position: fixed;
  33. background-color: rgb(0, 0, 0, .5);
  34. top: 0;
  35. left: 0;
  36. right: 0;
  37. bottom: 0;
  38. }
  39. .modal .modal-body{
  40. position: fixed;
  41. background-color: #fff;
  42. padding: 1em;
  43. overflow: hidden;
  44. max-width: 20em;
  45. max-height: 13em;
  46. /* 水平垂直自动居中 */
  47. top: 0;
  48. left: 0;
  49. right: 0;
  50. bottom: 0;
  51. margin: auto;
  52. }
  53. /* 关闭按钮 */
  54. .modal .modal-body .close{
  55. position: absolute;
  56. top: 1.1em;
  57. right: 1.5em;
  58. width: 3em;
  59. height: 1.5em;
  60. }
  61. .modal .modal-body fieldset{
  62. padding:1em;
  63. }
  64. .modal .modal-body fieldset p{
  65. padding: .5em 0;
  66. }
  67. .modal .modal-body fieldset p label{
  68. display: inline-block;
  69. min-width: 3.5em;
  70. }
  71. .modal .modal-body fieldset p button{
  72. width: 3.5em;
  73. height: 2em;
  74. }
  • 引入的外部 js/modal.js 为
  1. const btn = document.querySelector('header button');
  2. const modal = document.querySelector('.modal');
  3. const close = document.querySelector('.close');
  4. btn.addEventListener('click', setModal, false);
  5. close.addEventListener('click', setModal, false);
  6. function setModal(ev) {
  7. ev.preventDefault();
  8. let status = window.getComputedStyle(modal, null).getPropertyValue('display');
  9. modal.style.display = status === 'none' ? 'block' : 'none';
  10. }
  • 附图,点击登录弹出模态框

登录弹出模态框1

  • 点击关闭按钮,关闭模态框

登录弹出模态框2

批改老师:天蓬老师天蓬老师

批改状态:合格

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

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

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