一、基本思路

1、先看最终的效果图:

2、实现原理:通过position:absolute(绝对定位)来定位每一个元素的位置,并且将当前列的高度记录下来方便下一个dom位置的计算

二、代码实现

1、版本一:根据思路实现基础版

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css布局-瀑布流的实现</title>
<style type="text/css">
.box {
position: relative;
width: 500px;
min-height: 100px;
margin: 100px auto;
background: #eeeeee;
}
.item {
position: absolute;
width: 120px;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box">
<div class="item" style="height: 40px;background: red;"></div>
<div class="item" style="height: 50px;background: blue;"></div>
<div class="item" style="height: 100px;background: green;"></div>
<div class="item" style="height: 60px;background: gray;"></div>
<div class="item" style="height: 50px;background: orange;"></div>
<div class="item" style="height: 20px;background: yellow;"></div>
<div class="item" style="height: 40px;background: red;"></div>
<div class="item" style="height: 50px;background: blue;"></div>
<div class="item" style="height: 100px;background: green;"></div>
<div class="item" style="height: 120px;background: gray;"></div>
<div class="item" style="height: 58px;background: orange;"></div>
<div class="item" style="height: 36px;background: yellow;"></div>
</div>
<script type="text/javascript">
const BOX_WIDTH = document.querySelector('.box').offsetWidth //瀑布流外层盒子的宽度
const ITEM_WIDTH = document.querySelector('.item').offsetWidth //瀑布流内层盒子的宽度
const COLUMN = Math.floor(BOX_WIDTH/ITEM_WIDTH) //根据宽度计算可渲染的列数
const MARGIN = (BOX_WIDTH - ITEM_WIDTH*COLUMN)/(COLUMN-1) // 根据宽度计算每一列的间距
const MARGINTOP = 10 //固定设置每一个小盒子上下间距是10
let height_arr = new Array(COLUMN).fill(0) //定义一个长度等同与列数的数组用来存储每一列的高度,初始值均为0
let item = document.querySelectorAll('.item')
//遍历每一个小盒子,确定小盒子的位置
for(let i = 0; i < item.length; i++) {
let index = height_arr.indexOf(Math.min.apply(null, height_arr))
item[i].style.left = (ITEM_WIDTH + MARGIN) * index + 'px'
item[i].style.top = height_arr[index] + MARGINTOP + 'px'
height_arr[index] += item[i].offsetHeight + MARGINTOP
}
//将数组中最大的值,即最高的那一列的高度赋给外层盒子
document.querySelector('.box').style.height = Math.max.apply(null, height_arr) + 'px'
</script>
</body>
</html>

2、版本二:对版本一进行封装,方便重复使用

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css布局-瀑布流的实现</title>
<style type="text/css">
.box {
position: relative;
width: 500px;
min-height: 100px;
margin: 100px auto;
background: #eeeeee;
}
.item {
position: absolute;
width: 120px;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box" style="">
<div class="item" style="height: 40px;background: red;"></div>
<div class="item" style="height: 50px;background: blue;"></div>
<div class="item" style="height: 100px;background: green;"></div>
<div class="item" style="height: 60px;background: gray;"></div>
<div class="item" style="height: 50px;background: orange;"></div>
<div class="item" style="height: 20px;background: yellow;"></div>
   <div class="item" style="height: 40px;background: red;"></div>
   <div class="item" style="height: 50px;background: blue;"></div>
   <div class="item" style="height: 100px;background: green;"></div>
   <div class="item" style="height: 120px;background: gray;"></div>
   <div class="item" style="height: 58px;background: orange;"></div>
   <div class="item" style="height: 36px;background: yellow;"></div>
</div>
<script type="text/javascript">
function WaterFall(params) {
this.box = (params && params.parent) || '.box'
this.item = (params && params.child) || '.item'
this.column = (params && params.column) || 0
this.row_margin = (params && params.row_margin) || 0
this.column_margin = (params && params.column_margin) || 10
this.height_arr = []
this._box_width = 0
this._item_width = 0
this._computed = function() {
this._box_width = document.querySelector(this.box).offsetWidth
this._item_width = document.querySelector(this.item).offsetWidth
this.column = Math.floor((this._box_width - this.row_margin)/this._item_width) //列数
this.row_margin = !this.row_margin ? (this._box_width - this._item_width * this.column)/(this.column-1) : this.row_margin
}
this.init = function() {
this._computed()
let item = document.querySelectorAll(this.item)
this.height_arr = new Array(this.column).fill(0)
for(let i = 0; i < item.length; i++) {
let index = this.height_arr.indexOf(Math.min.apply(null, this.height_arr))
item[i].style.left = (this._item_width + this.row_margin) * index + 'px'
item[i].style.top = this.height_arr[index] + this.column_margin + 'px'
this.height_arr[index] += item[i].offsetHeight + this.column_margin
}
document.querySelector('.box').style.height = Math.max.apply(null, this.height_arr) + 'px'
}
}
var test = new WaterFall()
test.init()
</script>
</body>
</html>

三、总结:瀑布流的实现并不复杂,只要清楚了原理剩下的就是耐心的计算间距以及小盒子的位置啦~

css布局-瀑布流的实现的更多相关文章

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

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

  2. js-实现多列布局(瀑布流)

    本文是使用面向对象的思想实现多列布局(瀑布流).当然,使用面向过程也能实现,具体效果图和案例如下: 具体实现代码如下: <!DOCTYPE html> <html lang=&quo ...

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

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

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

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

  5. css3多列布局瀑布流加载样式

    看了一些网站的瀑布流加载,正好看到css3的多列属性,尝试着写了一个css做布局的瀑布流. 直接上代码: <!DOCTYPE html> <html lang="en&qu ...

  6. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

  7. Objectiv-c - UICollectionViewLayout自定义布局-瀑布流

    最近刚写的一个简单的瀑布流. 整体思路可能不是很完善. 不过也算是实现效果了. 高手勿喷 思路: 自定义UICollectionViewLayout实际上就是需要返回每个item的fram就可以了. ...

  8. 分别用js和css实现瀑布流

    下午查找了瀑布流的相关原理,找了一些css3实现的还有js实现的,最后总结了一些比较简单的,易懂的整理起来 1.css3实现 只要运用到    column-count分列 column-width固 ...

  9. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形...)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

随机推荐

  1. PAT_A1092#To Buy or Not to Buy

    Source: PAT A1092 To Buy or Not to Buy (20 分) Description: Eva would like to make a string of beads ...

  2. 拯救 Out Of Memory,8个案例带你飞!

    来自:唐尤华 https://bloggceasy.files.wordpress.com/2015/05/outofmemoryerror2.pdf 1. Java 堆空间 发生频率:5颗星 造成原 ...

  3. 结合Poi实现可读取Excel的文件选择对话框

    第一步:ApachePoi的jar包导全,不全会出现异常. 第二步:写就完事了:此例为读取特定模板的excel,仅供参考,根据实际需求改写. package 自建包; import java.awt. ...

  4. 国内pypi镜像

    V2EX pypi.v2ex.com/simple 豆瓣 http://pypi.douban.com/simple 阿里云(推荐使用) http://mirrors.aliyun.com/pypi/ ...

  5. JAVA集合--Collection接口

        本文首发于cartoon的博客     转载请注明出处:https://cartoonyu.github.io/cartoon-blog     在概述里面也说过:Collection是jav ...

  6. Xn数列

     题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输 ...

  7. mysql 记录(record)

    以下内容来源于<mysql内核:Innodb存储引擎 卷1> 简单介绍物理记录和大记录.仅为理解mysql 索引基础 存储结构这一章节而写. mysql的默认存储引擎为Innodb.Inn ...

  8. http11.Http11OutputBuffer.SocketOutputBuffer.doWrite

    这是一个错误. 我在spring框架中,创建了一个基类SuperBaseController, 并且使用了@ModelAttribute用来给HttpServletRequest和HttpServle ...

  9. Class类的作用?生成Class对象的方法有哪些?

    Class类是Java 反射机制的起源和入口,用于获取与类相关的各种信息,提供了获取类信息的相关方法.Class类继承自Object类 Class类是所有类的共同的图纸.每个类有自己的对象,好比图纸和 ...

  10. selenium 显示等待、隐式等待、强制等待

    如今大部分web程序使用Ajax技术,当浏览器加载页面时,页面元素可能不是同时加载完成,如果因为加载某个元素超时导致ElementNotVisibleException的情况出现,自动化脚本的稳定性就 ...