原生 JavaScript 瀑布流

一、原生 JavaScript 瀑布流

<!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>

    <style>

        * {

            margin: 0;

            padding: 0;

        }



        .waterfall {

            width: 100vw;

            height: 100vh;

        }



        .waterfall-container {

            width: 25vw;

            box-sizing: border-box;

            /* border: 1px solid #000; */

            border-right: 10px solid #fff;

            border-left: 10px solid #fff;

            float: left;

            /* background-color: pink; */

        }



        .waterfall-item {

            width: 100%;

            /* height: 200px; */

            background-color: pink;

            margin-bottom: 20px;

        }

    </style>

</head>



<body>

    <div class="waterfall">

        <div class="waterfall-container">

            <!-- <div class="waterfall-item"></div> -->

        </div>

        <div class="waterfall-container">



        </div>

        <div class="waterfall-container">



        </div>

        <div class="waterfall-container">



        </div>

    </div>

    <script>

        function rnd(n, m) {

            return parseInt(Math.random() * (m - n) + n);

        }

        var aUl = document.querySelectorAll('.waterfall-container');

        function creatDiv() {

            var oDiv = document.createElement('div');

            oDiv.className = 'waterfall-item';

            oDiv.style.height = rnd(200, 400) + 'px';

            oDiv.style.background = `rgb(${rnd(0, 256)},${rnd(0, 256)},${rnd(0, 256)})`

            return oDiv;

        }

        function createDiv20() {

            for (var i = 0; i < 20; i++) {

                var oDiv = creatDiv();

                var arr = []

                for (var j = 0; j < aUl.length; j++) {

                    arr.push(aUl[j]);

                }

                arr.sort(function (ul1, ul2) {

                    return ul1.offsetHeight - ul2.offsetHeight

                })

                // console.log(arr);

                arr[0].appendChild(oDiv)

            }

        }

        createDiv20()

        // console.log(rnd(1, 100))

        document.onscroll = function () {

            var scrollTop = document.documentElement.scrollTop;

            var viewHeight = document.documentElement.clientHeight;

            var bodyHeight = document.body.scrollHeight;

            console.log(scrollTop + viewHeight, bodyHeight);

            if (Math.ceil(scrollTop + viewHeight) >= bodyHeight) {

                createDiv20()

            }

        }



    </script>

</body>



</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值