要求
多个等宽的图片进行穿插排序成六列,并且在目前显示的最后一张图片显示之后,再进行下拉时能够在进行加载其他图片,每次下拉显示20-30张图片。
原理
页面上也有多等宽的块(块就页面内容),每一块都是绝对定位(absolute),每个块排列的方式如下:寻找现在高度最小的列,把该块定位到该列下方
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221113230729_1.jpg" alt="">
</div>
<div class="box">
<img src="./300 (3).webp" alt="">
</div>
<div class="box">
<img src="./20221209182724_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
<div class="box">
<img src="./20221210133951_1.jpg" alt="">
</div>
<div class="box">
<img src="./20221209182715_1.jpg" alt="">
</div>
<div class="box">
<img src="./300.webp" alt="">
</div>
</div>
</body>
</html>
css
* {
margin: 0;
padding: 0;
}
img {
display: block;
}
.container {
width: 100%;
position: relative;
}
.box {
float: left;
}
.box img {
padding: 5px;
width: 240px;
height: auto;
}
js
<script>
window.onload = function () {
//获取container
var container = document.getElementsByClassName("container")
//获取container的宽度
var containerWidth = innerWidth
// 获取每一张需要的图片
var imgs = document.querySelectorAll("img")
// 获取图片宽度
var imgWidth = imgs[0].offsetWidth
// 获取第一行的图片数量
var sum = Math.floor(containerWidth / imgWidth)
// 创建数组储存第一行图片的宽度
var heightArr = []
for (var i = 0; i < imgs.length; i++) {
if (i < sum) {
// 存入第一行图片的高度
heightArr.push(imgs[i].offsetHeight)
} else {
var minHeight = heightArr[0]
var minIndex = 0
for (var j = 0; j < heightArr.length; j++) {
if (heightArr[j] < minHeight) {
// 更正最小高度和索引
minHeight = heightArr[j]
minIndex = j
}
}
// 给后续图片加上定位
imgs[i].style.position = "absolute"
imgs[i].style.top = minHeight + "px"
imgs[i].style.left = imgs[minIndex].offsetLeft + "px"
heightArr[minIndex] = heightArr[minIndex] + imgs[i].offsetHeight
}
}
}
</script>
瀑布流布局