下午查找了瀑布流的相关原理,找了一些css3实现的还有js实现的,最后总结了一些比较简单的,易懂的整理起来

1.css3实现

只要运用到    column-count分列

      column-width固定宽度

      column-gap设置列间的间隔

      break-inside:avoid防止换行

注意: Internet Explorer 9及更早 IE 版本浏览器不支持 column-count 属性。

下面是一个例子:

html代码:

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta charset="utf-8" />
  6. <title>css3实现简单的瀑布流</title>
  7. <link rel="stylesheet" href="css/index.css" />
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="waterfall">
  12. <div class="pin">
  13. <img src="http://dummyimage.com/640x4:3" />
  14. <p>1 convallis timestamp</p>
  15. </div>
  16.  
  17. <div class="pin">
  18. 2 Donec a fermentum nisi.
  19. </div>
  20.  
  21. <div class="pin">
  22. <img src="http://dummyimage.com/640x3:4" />
  23. <p>
  24. 3 Nullam eget lectus augue. Donec eu sem sit amet ligula faucibus suscipit. Suspendisse rutrum turpis quis nunc convallis quis aliquam mauris suscipit.
  25. </p>
  26. </div>
  27.  
  28. <div class="pin">
  29. <img src="http://loremflickr.com/640/480/germany" />
  30. <p>
  31. 4 Donec a fermentum nisi. Integer dolor est, commodo ut sagittis vitae, egestas at augue.
  32. </p>
  33. </div>
  34.  
  35. <div class="pin">
  36. <img src="http://dummyimage.com/480x4:3" />
  37. <p>
  38. 5 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed feugiat consectetur pellentesque. Nam ac elit risus, ac blandit dui. Duis rutrum porta tortor ut convallis. Duis rutrum porta tortor ut convallis.
  39. </p>
  40. </div>
  41.  
  42. <div class="pin">
  43. <img src="http://dummyimage.com/480x3:4" />
  44. <p>
  45. 6 Nullam eget lectus augue. Donec eu sem sit amet ligula faucibus suscipit. Suspendisse rutrum turpis quis nunc convallis quis aliquam mauris suscipit. Duis rutrum porta tortor ut convallis.
  46. </p>
  47. </div>
  48.  
  49. <div class="pin">
  50. <img src="http://dummyimage.com/640x21:10" />
  51. <p>
  52. 7 Nullam eget lectus augue.
  53. </p>
  54. </div>
  55.  
  56. <div class="pin">
  57. <p>
  58. 8 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed feugiat consectetur pellentesque.
  59. </p>
  60. </div>
  61.  
  62. <div class="pin">
  63. <img src="http://dummyimage.com/800x4:3" />
  64. <p>
  65. 9 Donec a fermentum nisi. Integer dolor est, commodo ut sagittis vitae, egestas at augue. Suspendisse id nulla ac urna vestibulum mattis. Duis rutrum porta tortor ut convallis.
  66. </p>
  67. </div>
  68.  
  69. <div class="pin">
  70. <img src="http://dummyimage.com/900x4:2" />
  71. <p>
  72. 10 Donec a fermentum nisi. Integer dolor est, commodo ut sagittis vitae, egestas at augue. Suspendisse id nulla ac urna vestibulum mattis.
  73. </p>
  74. </div>
  75.  
  76. <div class="pin">
  77. <img src="http://dummyimage.com/640x5:4" />
  78. <p>
  79. 11 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed feugiat consectetur pellentesque. Nam ac elit risus, ac blandit dui. Duis rutrum porta tortor ut convallis.
  80. </p>
  81. </div>
  82.  
  83. <div class="pin">
  84. <img src="http://loremflickr.com/1024/768/cool" />
  85. <p>
  86. 12 Donec a fermentum nisi. Integer dolor est, commodo ut sagittis vitae, egestas at augue. Suspendisse id nulla ac urna vestibulum mattis.
  87. </p>
  88. </div>
  89.  
  90. <div class="pin">
  91. <img src="http://dummyimage.com/800x16:3" />
  92. <p>
  93. 13 Donec a fermentum nisi. Integer dolor est, commodo ut sagittis vitae, egestas at augue. Suspendisse id nulla ac urna vestibulum mattis.
  94. </p>
  95. </div>
  96.  
  97. <div class="pin">
  98. <img src="http://loremflickr.com/1024/768/vw,golf,variant/all" />
  99. <p>
  100. 14 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed feugiat consectetur pellentesque. Nam ac elit risus, ac blandit dui. Duis rutrum porta tortor ut convallis.
  101. </p>
  102. </div>
  103.  
  104. <div class="pin">
  105. <img src="http://dummyimage.com/300x4:3" />
  106. <p>
  107. 15 Nullam eget lectus augue.
  108. </p>
  109. </div>
  110.  
  111. <div class="pin">
  112. <img src="http://dummyimage.com/640x16:9" />
  113. <p>
  114. 16 Nullam eget lectus augue.
  115. </p>
  116. </div>
  117. </div>
  118. </div>
  119. </body>
  120.  
  121. </html>

css代码:

  1. body {
  2. background-color: #f6f6f6;
  3. }
  4.  
  5. .container {
  6. width: 80%;
  7. margin: 0 auto;
  8. }
  9.  
  10. .waterfall {
  11. -moz-column-count:;
  12. -webkit-column-count:;
  13. column-count:;
  14. -moz-column-width: 24em;
  15. -webkit-column-width: 24em;
  16. column-width: 24em;
  17. -moz-column-gap: 1em;
  18. -webkit-column-gap: 1em;
  19. column-gap: 1em;
  20. }
  21.  
  22. .pin {
  23. padding: 1em;
  24. margin: 0 0.125em 1em;
  25. -moz-page-break-inside: avoid;
  26. -webkit-column-break-inside: avoid;
  27. break-inside: avoid;
  28. background: white;
  29. -moz-box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 1px 2px 0 rgba(0, 0, 0, 0.24);
  30. -webkit-box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 1px 2px 0 rgba(0, 0, 0, 0.24);
  31. box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 1px 2px 0 rgba(0, 0, 0, 0.24);
  32. }
  33.  
  34. .pin img {
  35. width: 100%;
  36. padding-bottom: 1em;
  37. margin-bottom: 0.5em;
  38. border-bottom: 1px solid #cccccc;
  39. }

效果实现:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

js实现瀑布流

原理:

     1.编写方法:获取容器内所有外层元素,存入数组;

2.编写方法:计算容器内一行可以承载多少个元素,方法:容器宽度/元素宽度,四舍五入向下取整;

3.编写方法:把每一行中所有元素的高度值存入数组;

4.编写方法:在高度值数组中找到最小高度值;

5.编写方法:把第二行第一个元素定位到上一行所有元素中高度最低的元素下面,即设置该元素的top,left,position属性;

6.编写方法:重置当前高度值数组中的最小值;

7.编写方法:从第二行第一个元素开始,遍历每个元素,用上述方法重新定位每个元素的位置,把该事件绑定到页面;

8.编写方法:监听鼠标事件,当前容器内最下面一个元素的offsetTop<浏览器可视高度+已滚动高度时,加载下一页数据;

9.加载完之后,用上述方法重新定位新加载元素的位置;

下面是一个例子:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>瀑布流</title>
  6. <style type="text/css">
  7. div{
  8. background: #ccc;
  9. width: 200px;
  10. position: absolute;
  11. transition:0.5s;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16.  
  17. <script>
  18. //js代码
  19. </script>
  20. </body>
  21. </html>

这里的模拟瀑布流里面的图片全用一个div去模拟了,每个div宽度(200px)都一样,跟上面的图片例子一样(高度可能不一样),然后将每一个div都绝对定位,因为在后面会通过left、top值改变div的位置。transition:0.5s;只是给div改变样式时一个过渡。

  1. <script type="text/javascript">
  2. createDiv ()
  3. function createDiv () {
  4. for (var i = 0; i < 20; i++) {
  5. var div = document.createElement('div');
  6. var rnd = Math.floor(Math.random()*300+50) //div的高度在50到350之间
  7. div.style.height = rnd + "px";
  8. div.innerHTML = i;
  9. document.body.appendChild(div);
  10. };
  11. change()
  12. }
  13.  
  14. function change() {
  15. var aDiv = document.getElementsByTagName('div');
  16. // alert(aDiv.length);
  17. var windowCW = document.documentElement.clientWidth; //窗口视口的宽度
  18. var n = Math.floor(windowCW/210); //一行能容纳多少个div,并向下取整
  19. if (n<=0) {return};
  20. // alert(n);
  21. var t = 0;
  22. var center = (windowCW - n*210)/2; //居中
  23. var arrH = []; //定义一个数组存放div的高度
  24. for (var i = 0; i < aDiv.length; i++) {
  25. var j = i%n;
  26.  
  27. if (arrH.length==n) { //一行排满n个后到下一行
  28. var min = findMin(arrH); //从最“矮”的排起,可以从下图的序号中看得出来,下一行中序号是从矮到高排列的
  29. aDiv[i].style.left =center + min*210 + "px";
  30. aDiv[i].style.top = arrH[min]+10 + "px";
  31. arrH[min] += aDiv[i].offsetHeight + 10;
  32. // alert(min);
  33. }else{
  34. arrH[j] = aDiv[i].offsetHeight;
  35. aDiv[i].style.left =center + 200*j+10*j + "px";
  36. aDiv[i].style.top = 0;
  37. }
  38.  
  39. };
  40. }
  41. window.onresize = function(){ //窗口改变也调用函数
  42. change();
  43. }
  44. window.onscroll= function () {
  45. // 页面总高度
  46. var bodyHeight = document.documentElement.offsetHeight;
  47. // 可视区高度
  48. var windowHeight = document.documentElement.clientHeight;
  49. //滚动条的高度
  50. var srcollTop = document.documentElement.scrollTop || document.body.scrollTop;
  51. var srcollH = document.body.scrollHeight;
  52. // alert(srcollH);
  53. if (srcollTop+windowHeight >= srcollH-20) {
  54. createDiv();
  55. };
  56. }
  57.  
  58. function findMin(arr) {
  59. var m = 0;
  60. for (var i = 0; i < arr.length; i++) {
  61. m = Math.min(arr[m], arr[i]) == arr[m] ? m : i;
  62. }
  63. return m;
  64. }
  65. </script>

分别用js和css实现瀑布流的更多相关文章

  1. 简单CSS定位瀑布流实现方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  3. css布局-瀑布流的实现

    一.基本思路 1.先看最终的效果图: 2.实现原理:通过position:absolute(绝对定位)来定位每一个元素的位置,并且将当前列的高度记录下来方便下一个dom位置的计算 二.代码实现 1.版 ...

  4. js实现图片的瀑布流

    先看效果: 初始状态:

  5. 利用JS实现简单的瀑布流效果

    哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...

  6. 详解纯css实现瀑布流(multi-column多列及flex布局)

    瀑布流的布局自我感觉还是很吸引人的,最近又看到实现瀑布流这个做法,在这里记录下,特别的,感觉flex布局实现瀑布流还是有点懵的样子,不过现在就可以明白它的原理了 1.multi-column多列布局实 ...

  7. 玩转Masonry JS库来实现瀑布流Web效果

    工作项目中需要制作个Mobile上的Web App的展示,方便快捷访问和评价反馈.在展示页面能看到应用展示图,点击进入Web应用.我不是前端开发者,对HTML, CSS, JS这三剑客仅仅是略知一二. ...

  8. 纯css实现瀑布流(multi-column多列及flex布局)

    瀑布流的布局自我感觉还是很吸引人的,最近又看到实现瀑布流这个做法,在这里记录下,特别的,感觉flex布局实现瀑布流还是有点懵的样子,不过现在就可以明白它的原理了 1.multi-column多列布局实 ...

  9. 【js】【图片瀑布流】js瀑布流显示图片20180315

    js实现把图片用瀑布流显示,只需要“jquery-1.11.2.min.js”. js: //瀑布流显示图片 var WaterfallImg = { option: { maxWidth: 850, ...

随机推荐

  1. 关于python中的循环

    参考下面这一篇: http://www.cnblogs.com/vamei/archive/2012/05/30/2526357.html 其中用range的方式最好!

  2. 【转】简明 Python 教程

    原文网址:http://woodpecker.org.cn/abyteofpython_cn/chinese/ 简明 Python 教程Swaroop, C. H. 著沈洁元  译www.byteof ...

  3. Hibernate Cannot release connection 了,有办法解决!

      问题:    系统采用Spring MVC 2.5 + Spring 2.5 + Hibernate 3.2架构,其中数据源连接池采用的是Apache commons DBCP.问题是这样的,系统 ...

  4. 最近github上的一些有用链接资料备份

    https://github.com/dlunion 这个人写了库里面有caffe简单版本的代码,依赖少的版本,他的caffe可以支持ssd和lstm序列识别等等 还有openPose等库代码 OCR ...

  5. 3、MR开发入门

    1.预先准备2个文件file1.txt和file2.txt.文件内容为网页上摘下,不具有代表性,只为举例. file1.txt内容为: With this setup, whenever you ch ...

  6. kotlin学习一:kotlin简介

    kotlin是JetBrains公司出品的基于JVM的语言,和其他JVM语言一样,目的在于提供比JAVA更加简介的语法, 同时提供函数式编程,不需要再像JAVA一样所有的一切都要依托于类. kotli ...

  7. 1103 Integer Factorization

    题意:给出一个正整数N,以及k,p,要求把N分解成k个因式,即N=n1^p + n2^p + ... + nk^p.要求n1,n2,...按降序排列,若有多个解,则选择n1+n2+...+nk最大的, ...

  8. 第十三章 hadoop机架感知

    背景 分布式的集群通常包含非常多的机器,由于受到机架槽位和交换机网口的限制,通常大型的分布式集群都会跨好几个机架,由多个机架上的机器共同组成一个分布式集群.机架内的机器之间的网络速度通常都会高于跨机架 ...

  9. Jquery获取用户控件页面中控件的值

    $('#<%= txt_P_name.ClientID%>').val()

  10. Druid.io系列(八):部署

    介绍 前面几个章节对Druid的整体架构做了简单的说明,本文主要描述如何部署Druid的环境 Imply提供了一套完整的部署方式,包括依赖库,Druid,图形化的数据展示页面,SQL查询组件等.本文将 ...