<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>瀑布流</title>
<link rel="stylesheet" href="../css/reset.css" />
<style>
* {
margin: 0;
padding: 0;
}
.bigbox {
width: 900px;
margin: 0 auto;
background: #3d4a65;
min-height: 200px;
position: relative;
}
.picbox {
/*图片盒子*/
/*边框2px*/
padding: 2px;
float: left;
overflow: hidden;
/*position:absolute;*/
}
img {
/*图片自动大小缩放*/
width: 200px;
}
</style>
</head>
<body>
<script>
//要全部加载完后再计算 并获取容器的宽度
//要点:1、第一行的元素加浮动 其余行的元素加绝对定位
//2、空数组记录第一行元素的高度 并将接下来的元素接到数组中最小的下标部分 定位
// 3、更新数组高度:preHeight+elementHeight=newHeight;
window.onload = function () {
let bigBox = document.querySelector(".bigbox"); //大盒子
let picbox = document.querySelectorAll(".picbox"); //存放图片的盒子
let bigBoxWidth = bigBox.clientWidth; //大框内容区宽度
let picWidth = picbox[0].offsetWidth; //图片容器的内容宽度+padding+border的宽度
// 计算每行能放下的列数
let colNum = Math.floor(bigBoxWidth / picWidth); //向下取整
//每colNum张图片放在一行
let colHightArr = []; //定义一个存行高的数组
picbox.forEach((pic, index) => {
if (index >= colNum) {
//加绝对定位 使其放在每行最短的部分
for (let i = index; i < picbox.length; i++) {
// 第一行以后的元素全部加绝对定位
picbox[i].style.position = "absolute";
}
} else {
//第一行的时候 将每张图片的高度都存入数组
colHightArr.push(picbox[index].offsetHeight);
}
});
let short = findmin(colHightArr); //找到最短下标
position(); //图片设置定位
setHeight(); //定位完毕后给父元素(最外层包裹的大盒子)加高度
function position() {
// 定位函数
for (let i = colNum; i < picbox.length; i++) {
//循环除了第一行的所有图片\
//找到最短的地方定位
picbox[i].style.left = picWidth * short + "px";
picbox[i].style.top = colHightArr[short] + "px";
let addPicHeight = picbox[i].offsetHeight; //新加入图片的高度
let newHeight = addPicHeight + colHightArr[short];
colHightArr.splice(short, 1, newHeight); //替换元素
short = findmin(colHightArr); //再次找到最短的下标 进入下一次循环
}
}
function findmin(arr) {
//找到最短下标函数
let short = arr.indexOf(Math.min(...arr)); //寻找最短高度的下标索引值
return short; //返回最短的下标
}
function setHeight() {
//解决父元素会高度塌陷的问题
let long = Math.max(...colHightArr); //找到最长的元素
bigBox.style.height = long + "px";
}
};
</script>
<div class="bigbox clean">
<!-- 外边框-->
<div class="picbox">
<img src="../img/1.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/2.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/3.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/4.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/5.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/6.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/7.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/8.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/9.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/10.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/11.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/12.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/13.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/14.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/15.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/16.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/17.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/18.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/19.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/20.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/21.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/22.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/23.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/24.jpg" alt="" />
</div>
<div class="picbox">
<img src="../img/25.jpg" alt="" />
</div>
</div>
</body>
</html>
JS瀑布流
最新推荐文章于 2024-11-14 17:44:13 发布