js实现瀑布流的效果

文件目录
在这里插入图片描述
index.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>
    <script src="./js.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #waterfull {
            width: 1200px;
            height: auto;
            position: relative;
        }

        .box {
            float: left;
            padding: 5px;
        }

        .box_image {
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 5px 5px 8px #ccc
        }

        #waterfull img {
            width: 278px;
            height: auto;
        }
    </style>
</head>

<body>
    <div id="waterfull">
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=676339547,505086010&fm=253&fmt=auto&app=138&f=JPEG?w=334&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=676339547,505086010&fm=253&fmt=auto&app=138&f=JPEG?w=334&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=1960110688,1786190632&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F72ce0dcb-c2a3-49fd-933d-85c7e5c4533d%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=51028736bd7b563f7d5e526e91ddb503"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img2.baidu.com/it/u=3853345508,384760633&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1200"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fa3ea80a8-4873-4346-b691-6dd773b8491a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=3e394623a1e12b6151d73c75ff87193c"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img1.baidu.com/it/u=1960110688,1786190632&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F4045f4e3-30ff-46b9-961c-01abbb9dc87c%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=af81a586e26288d64db5dfc9a59dcaf2"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fb47f4c14-8651-410b-adcd-961ca31c0780%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=521719cbfeeaf1c4bbc3d055fbf1a789"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F4045f4e3-30ff-46b9-961c-01abbb9dc87c%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=af81a586e26288d64db5dfc9a59dcaf2"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F4045f4e3-30ff-46b9-961c-01abbb9dc87c%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=af81a586e26288d64db5dfc9a59dcaf2"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c0b51bd-6613-4096-bfe6-17b59ee1ea1a%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1686368557&t=27e74f5d7be3d760b13bebb2f858acda"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://img0.baidu.com/it/u=1604010673,2427861166&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        <div class="box">
            <div class="box_image"><img
                    src="https://picnew12.photophoto.cn/20180805/zongyikatongjianyuexiaoqingxinbeijing-32856723_1.jpg"
                    alt=""></div>
        </div>
        

    </div>

</body>

</html>

js代码

window.onload = function () {
    images_location("waterfull", "box");
    function images_location(Parent, Children) {
        var parent = document.getElementById(Parent);//获取id为waterfull的元素
        var children = getChildElement(parent, Children);//通过一个封装函数来获取上述获取到的元素的所有符合条件的子元素,获得一个数组
        var imageWidth = children[0].offsetWidth;//因为在css中已经设定了固定宽度,所以只需要第一张图片的宽度。
        var cols = Math.floor(1250 / imageWidth);
        // 获取在固定宽度的情况下可以放下的列数。当然这里可以不用固定宽度,而使用用户浏览器的宽度document.documentElement.clientWidth。
        var hArr = [];
        for (var i = 0; i < children.length; i++) {
            if (i < cols) {
                hArr.push(children[i].offsetHeight);//将第一列中的图片高度数据存到hArr数组中
            } else {
                var minH = Math.min.apply(null, hArr);//又有Math.min方法的参数必须是一个一个的数,故这里使用apply方法传入数组求得最小的高度
                var index = getMinhIndex(hArr, minH);//封装一个函数获得最小高度的图片所在列的索引
                children[i].style.position = "absolute";//使用绝对定位
                children[i].style.top = minH + "px";//注意:这里一定要有px这个单位,否则将会出错
                children[i].style.left = imageWidth * index + "px";//即left的值
                hArr[index] += children[i].offsetHeight;//将刚添加的那一列的offsetHeight值更新
            }

        }

    }
    function getChildElement(Parent, Children) {
        var childrenArr = [];
        var allChildren = Parent.getElementsByTagName("*");
        for (var i = 0; i < allChildren.length; i++) {
            if (allChildren[i].className == Children) {//注意:原生JavaScript中对于元素由className这个属性,即获取类名。
                childrenArr.push(allChildren[i]);
            }
        }
        return childrenArr;
    }
    function getMinhIndex(arr, val) {
        for (var i in arr) { //遍历arr数组中的每一个元素,其中i表示各元素的索引
            if (arr[i] == val) {
                return i;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值