Blogger Information
Blog 145
fans 7
comment 7
visits 164086
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
阐述float和position的作用,并利用定位做一个拟态框案例
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
935 people have browsed it

浮动属性和位置属性的作用

一、浮动相关知识:float

1、float:常见值值right|left|none;
2、float常用来解决图文混排问题和布局,左右只限于水平方向
3、float设置浮动后,会脱离文档流,变成行内块,且容易造成父级塌陷
4、解决父级塌陷问题:利用兄弟级元素设置clear:both|right|left来清除浮动,但增加元素cssm美化布局有一定影响,故可以通过伪类来设置清除浮动例如:div:after|before;现在常用的解决父级塌陷的方法是:父级设置overflow:auto|scroll|hidden

二、位置属性的使用:position

1、position:常见的值relative|absolute|fixed
2、设置relative可以通过top|right|left|bottom来设置元素的位置相对于元素的原始位置偏移,也可以通过四个方向设置0来让子元素充满父级元素
3、设置absolute也可通过top|right|bottom|left来设置元素的位置,不过它是相对于离自己最近设置position:relative|absolute的父级的位置来偏移;如果没有则以html根元素来偏移
3、设置fixed后是相对于根元素来设置的固定位置,通常来设置回到顶部和浮动广告

拟态登陆的案例-小demo

1.代码:

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>拟态登陆小案例</title>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. header{
  15. padding: 0 20px;
  16. height: 80px;
  17. display: flex;
  18. flex-flow: row nowrap;
  19. justify-content: space-between;
  20. align-items: center;
  21. background-color: lightslategray;
  22. }
  23. header .lg button{
  24. border: none;
  25. background-color: lightskyblue;
  26. width: 80px;
  27. height: 40px;
  28. font-size:18px;
  29. color:white;
  30. font-weight: 400;
  31. border-radius: 10px;
  32. }
  33. header .lg button:hover{
  34. background-color: lightcoral;
  35. box-shadow: 0 0 3px #2C3E50;
  36. }
  37. .login-windows{
  38. background-color: #000000;
  39. opacity: 0.4;
  40. position: fixed;
  41. top:0;
  42. right:0;
  43. left:0;
  44. bottom:0;
  45. display: none;
  46. }
  47. .login{
  48. background-color: lightseagreen;
  49. width: 460px;
  50. height: 260px;
  51. position: absolute;
  52. top:0;
  53. right:0;
  54. left:0;
  55. bottom:0;
  56. margin:auto;
  57. }
  58. .login .lg-user{
  59. width: 200px;
  60. height: 180px;
  61. position: absolute;
  62. top:0;
  63. right:0;
  64. left:0;
  65. bottom:0;
  66. margin:auto;
  67. }
  68. .close {
  69. position: absolute;
  70. top:0;
  71. right: 0;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <header>
  77. <div class="logo">
  78. <a href="http://www.zhongyequan.com"><img src="image/网站首页logo.png" alt=""></a>
  79. </div>
  80. <div class="lg">
  81. <button>登陆</button>
  82. <button>注册</button>
  83. </div>
  84. </header>
  85. <div class="login-windows">
  86. <div class="login">
  87. <div class="lg-user">
  88. <h2>用户登陆</h2>
  89. <div>
  90. <label for="username">账号:</label>
  91. <input type="text" id="username" name="username" value="" required>
  92. </div>
  93. <div>
  94. <label for="">密码:</label>
  95. <input type="password" id='pwd' name="pwd" value="" required >
  96. </div>
  97. <div>
  98. <button type="submit">登陆</button>
  99. <button type="reset">重置</button>
  100. </div>
  101. </div>
  102. <div class="close">
  103. <button type="button">X</button>
  104. </div>
  105. </div>
  106. </div>
  107. </body>
  108. <script src="lg.js" type="text/javascript" charset="utf-8"></script>
  109. </html>

lg.js代码:

  1. const btn = document.querySelector('header .lg button:first-child');
  2. const modal = document.querySelector('.login-windows');
  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. }

运行结果:

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:老生的作业应该可以写得更优秀
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post