博客列表 >07 浮动元素、高度塌陷、三列页面

07 浮动元素、高度塌陷、三列页面

老黑
老黑原创
2020年06月19日 19:09:45548浏览

主要内容:

  • ① 通过js提升图片页面加载速度(懒加载原理)。
  • ② 元素浮动和位置让出。
  • ③ 左右浮动和免受影响。
  • ④ 高度塌陷及各种解决办法。
  • ⑤ 典型三列页面的实现(定位模式、浮动模式、圣杯模式、Grid模式)。

1、通过js提升图片页面加载速度

直接加载图片会导致页面加载速度放慢。于是可以先放一些用来占位的小图。然后等到图在页面中出现了(结合scroll这块来判断),再用js加载成正式的图。

  1. <body>
  2. <img src="temp.jpg" alt="" data-src="thief.png" />
  3. <img src="temp.jpg" alt="" data-src="thief.png" />
  4. <img src="temp.jpg" alt="" data-src="thief.png" />
  5. </body>
  6. <script>
  7. // 视口大小: 窗口大小
  8. const viewHeight = document.documentElement.clientHeight;
  9. // 监听文档对象的滚动事件
  10. document.addEventListener("scroll", showImg);
  11. // 初始化
  12. window.addEventListener("load", showImg);
  13. // 回调
  14. function showImg() {
  15. // 获取所有图片
  16. let imgs = document.querySelectorAll("img");
  17. // 遍历每一张图片,判断当前的图片是否进入到用户的可视区域
  18. imgs.forEach(function (img) {
  19. let imgTop = img.offsetTop;
  20. // 滚动高度 = 视口高度 + 滚动距离
  21. let scrTop = viewHeight + document.documentElement.scrollTop;
  22. // 图片已进入可视区
  23. if (imgTop < scrTop) {
  24. img.src = img.dataset.src;
  25. }
  26. });
  27. }
  28. </script>

2、元素浮动和位置让出

  1. 元素浮动之后从文档流中脱离出来(会释放它原来在文档流中占据的空间)
  2. 后面的元素会自动填充它出让出来的空间大小
  3. 浮动元素只会影响到它后面的元素布局,对前面没有影响
  4. 行内元素用于最终内容的载体, 不能充当容器/当父级,不能设置或设置了宽高也是无效的。例如a、p元素等
  5. 任何元素,一旦浮动都自动转为块级元素

3、左右浮动和免受影响

  1. float: left;
  2. float: right;
  3. clear: both;(免受前面浮动元素的影响)
  4. 当然也有:
  5. clear:left; clear:right;

4、高度塌陷及解决办法

什么是高度塌陷:父元素在文档流中高度默认是被子元素撑开的,当子元素脱离文档流以后,将无法撑起父元素的高度,也就会导致父元素的高度塌陷。

  1. /* 解决方案1: 给父元素也添加一个高度。缺点是无法随浮动元素的高度变化而变化*/
  2. .container {
  3. height: 150px;
  4. }
  5. /* 解决方案2: 把父元素也浮动起来 */
  6. .container {
  7. float: left;
  8. }
  9. .box {
  10. float: left;
  11. }
  12. /* 解决方案3: 添加一个专用元素用于清浮动 */
  13. div.clear {
  14. clear: both;
  15. }
  16. /* 解决方案4: 通过添加一个伪元素来解决 */
  17. .container::after {
  18. content: "";
  19. display: block;
  20. clear: both;
  21. }
  22. /* 解决方案5: 最简单的解决方案,用到BFC(块级格式化上下文) */
  23. .container {
  24. /* overflow: hidden; */
  25. overflow: auto;
  26. }

5、典型三列页面的实现(定位模式、浮动模式、圣杯模式、Grid模式)

方法1:定位模式
  1. <!--body中的块元素-->
  2. <div class="container">
  3. <div class="left">左侧</div>
  4. <div class="main">内容区</div>
  5. <div class="right">右侧</div>
  6. </div>
  7. <!--css中的左右定位等-->
  8. <style>
  9. /* 主体用定位 */
  10. .container {
  11. width: 960px;
  12. margin: 10px auto;
  13. /* background-color: #ccc; */
  14. min-height: 600px;
  15. /* 转为定位元素,做为定位父级 */
  16. position: relative;
  17. }
  18. .container > .left {
  19. width: 200px;
  20. background-color: wheat;
  21. min-height: 600px;
  22. position: absolute;
  23. top: 0;
  24. left: 0;
  25. }
  26. .container > .right {
  27. width: 200px;
  28. background-color: wheat;
  29. min-height: 600px;
  30. position: absolute;
  31. top: 0;
  32. right: 0; /*这块是以右侧为锚定点来进行定位*/
  33. }
  34. .container > .main {
  35. background-color: lightgreen;
  36. min-height: 600px;
  37. width: 540px;
  38. position: absolute;
  39. top: 0;
  40. left: 210px;
  41. }
  42. </style>
方法2:浮动模式
  1. <style>
  2. /* 主体用定位 */
  3. .container {
  4. width: 960px;
  5. margin: 10px auto;
  6. /* background-color: #ccc; */
  7. min-height: 600px;
  8. /* 防止浮动元素的高度塌陷 */
  9. overflow: hidden;
  10. }
  11. .container > .left {
  12. width: 200px;
  13. background-color: wheat;
  14. min-height: 600px;
  15. float: left;
  16. }
  17. .container > .right {
  18. width: 200px;
  19. background-color: wheat;
  20. min-height: 600px;
  21. float: right;
  22. }
  23. .container > .main {
  24. background-color: lightgreen;
  25. min-height: 600px;
  26. width: 540px;
  27. float: left;
  28. margin-left: 10px;
  29. }
  30. </style>
方法3:圣杯模式

先将两边做好,然后中间自动填充。

  1. <div class="container">
  2. <!-- 圣杯布局要求主体内容优先渲染 -->
  3. <div class="main">内容区</div>
  4. <div class="left">左侧</div>
  5. <div class="right">右侧</div>
  6. </div>
  7. <style>
  8. .container {
  9. border: 5px dashed;
  10. overflow: hidden;
  11. }
  12. .container > * {
  13. min-height: 400px;
  14. }
  15. /* 左右固定 */
  16. .container > .left,
  17. .container > .right {
  18. width: 200px;
  19. background-color: cyan;
  20. }
  21. /* 中间样式 */
  22. .container > .main {
  23. width: 100%;
  24. background-color: wheat;
  25. }
  26. /* 所有子元素浮动起来 */
  27. .container > * {
  28. float: left;
  29. }
  30. /* 使用内边距把左右二边的位置挤出来 */
  31. .container {
  32. /* padding-left: 200px;
  33. padding-right: 200px; */
  34. padding: 0 200px;
  35. }
  36. .container > .left {
  37. margin-left: -100%;
  38. /* 使用相对定位将它复位 */
  39. position: relative;
  40. top: 0;
  41. right: 200px;
  42. }
  43. .container > .right {
  44. margin-left: -200px;
  45. /* 使用相对定位将它复位 */
  46. position: relative;
  47. top: 0;
  48. left: 200px;
  49. }
  50. </style>
方法4:Grid模式
  1. <body>
  2. <div class="header">页眉</div>
  3. <div class="left">左侧</div>
  4. <div class="main">主体</div>
  5. <div class="right">右侧</div>
  6. <div class="footer">页脚</div>
  7. </body>
  8. <style>
  9. body {
  10. display: grid;
  11. grid-template-columns: 200px 1fr 200px;
  12. grid-template-rows: 40px 500px 40px;
  13. gap: 10px;
  14. }
  15. /* 参考线 */
  16. body > * {
  17. outline: 1px dashed red;
  18. background-color: lightcyan;
  19. }
  20. .header,
  21. .footer {
  22. grid-column-end: span 4;
  23. }
  24. </style>

6、操练:使用定位与浮动完成典型三列页面

  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>Document</title>
  7. <style>
  8. * {
  9. margin: 0px;
  10. padding: 0px;
  11. box-sizing: border-box;
  12. }
  13. li {
  14. list-style: none;
  15. }
  16. a {
  17. text-decoration: none;
  18. color: white;
  19. }
  20. .head, .foot{
  21. height: 40px;
  22. background-color: darkgoldenrod;
  23. width: 960px;
  24. text-align: center;
  25. line-height: 40px;
  26. margin: auto;
  27. }
  28. .head ul li{
  29. float: left;
  30. /* line-height: 40px; */
  31. padding:0 20px;
  32. }
  33. .head ul li:hover{
  34. background-color: darkgrey;
  35. color: red;
  36. }
  37. .head ul li{
  38. float: left;
  39. }
  40. .container{
  41. margin: 10px auto;
  42. min-height: 600px;
  43. position: relative;
  44. width: 960px;
  45. }
  46. .container> .left{
  47. width: 200px;
  48. background-color: rosybrown;
  49. min-height: 600px;
  50. position: absolute;
  51. top: 0;
  52. left: 0;
  53. }
  54. .container> .right{
  55. width: 200px;
  56. background-color: rosybrown;
  57. min-height: 600px;
  58. position: absolute;
  59. top: 0;
  60. right: 0;
  61. }
  62. .middle{
  63. background-color: skyblue;
  64. min-height: 600px;
  65. width: 540px;
  66. position: absolute;
  67. top: 0;
  68. left: 210px;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div class="head">
  74. <ul>
  75. <li><a href="">凡界</a></li>
  76. <li><a href="">妖界</a></li>
  77. <li><a href="">仙界</a></li>
  78. <li><a href="">无界</a></li>
  79. </ul>
  80. </div>
  81. <br>
  82. <div class="container">
  83. <div class="left">左妖怪</div>
  84. <div class="middle">中凡人</div>
  85. <div class="right">右神仙</div>
  86. </div>
  87. <div class="foot">空间无限转,时间常年流</div>
  88. </body>
  89. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议