微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css布局-瀑布流的实现

一、基本思路

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>

 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。