原生js简易瀑布流无限加载效果

html部分

<div class="pull">
        <ul></ul>
        <ul></ul>
        <ul></ul>
        <ul></ul>
</div>

css部分

<style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
        }

        body {
            /* 防止body塌陷 */
            padding: 1px;
            /* height: 3000px; */
        }

        .pull {
            width: 1200px;
            margin: 100px auto;
        }

        .pull>ul {
            float: left;
            width: 22%;
            margin-right: 4%;
        }

        .pull>ul:last-child {
            margin-right: 0;
        }

        .pull>ul>li {
            font-size: 35px;
            text-align: center;
            color: #fff;
        }
</style>

js部分

<script>
        // 1.进入页面,生成一行颜色块
        // 滚动条距离底部 = 内容高度(scrollHeight) - 可视区高度(clientHeight)
        var bodyEle = document.body;
        var htmlEle = document.documentElement;
        var count = 0;
        var uls = document.querySelectorAll('.pull>ul');
        // console.log(bodyEle.scrollHeight, htmlEle.clientHeight);
        while (bodyEle.scrollHeight - htmlEle.clientHeight < 200) {
            create();
        }
        // 2.滚动时,生成一行颜色块
        // 距离底部 < 100
        // 距离底部 = 内容高度 - 可视区高度 - 滚动距离
        window.onscroll = function () {
            if (bodyEle.scrollHeight - htmlEle.clientHeight - window.pageYOffset < 100) {
                create();
            }
        }
        // 创建一行颜色块
        function create() {
            for (var i = 0; i < 4; i++) {
                var li = document.createElement('li');
                // li.style.backgroundColor = 'red';
                // li.style.height = '100px';
                // li.style.lineHeight = '100px';
                var h = random(230, 260, 5);
                li.style.cssText = `
                    height: ${h}px;
                    line-height: ${h}px;
                    background-color: rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
                `
                li.innerHTML = ++count;
                // 追加到内容高度最小的ul中
                getMinul().append(li);
            }
        }
        // 获取高度最小的ul
        function getMinul() {
            var minIndex = 0; // 用于保存高度最小的ul对应的下标
            uls.forEach((item, index) => {
                // console.log(item, index);
                if (item.scrollHeight < uls[minIndex].scrollHeight) {
                    minIndex = index;
                }
            })
            return uls[minIndex];
        }
        // 获取随机数
        function random(min, max, n = 1) {
            // return Math.floor(Math.random() * (max - min + 1)) + min;
            return Math.floor(Math.random() * (max - min + 1) / n) * n + min;
        }
 </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值