js实现瀑布流布局

js实现瀑布流布局

要求

多个等宽的图片进行穿插排序成六列,并且在目前显示的最后一张图片显示之后,再进行下拉时能够在进行加载其他图片,每次下拉显示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>

瀑布流布局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值