css+js+intersectionObserver 实现无限延展瀑布流

这段代码展示了如何使用Intersection Observer API监听元素交叠状态,当特定元素与视口发生交叠时,动态生成并插入不同高度的span元素到两个预设的div中。这个例子涉及到网页布局、JavaScript事件监听和DOM操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            html,
            body {
                height: auto;
                margin: 0;
            }

            .body {
                display: flex;
                align-items: flex-start
            }

            #div1,
            #div2 {
                height: auto;
                width: 200px;
                margin-left: 3.5%;
                background-color: skyblue;
            }

            #div3 {
                position: relative;
                top: 50px;
                height: 5px;
                width: 100%;
            }

            span {
                display: inline-block;
                background-color: red;
                width: 100%;
                margin-top: 20px;
            }

        </style>
    </head>

    <body>
        <div class="body">
            <div id="div1">
                <span style="height: 356px;"></span>
                <span style="height: 356px;"></span>
                <span style="height: 356px;"></span>
            </div>
            <div id="div2">
                <span style="height: 356px;"></span>
                <span style="height: 356px;"></span>
            </div>
        </div>
        <div id="div3">
        </div>
        <script>
            let id1 = document.getElementById('div1');
            console.dir(id1);
            let id2 = document.getElementById('div2');
            let id3 = document.getElementById('div3');
            let intersectionObserver = new IntersectionObserver((list) => {
                if (list[0].target.id === "div3" && list[0].isIntersecting) {
                    console.log('list[0].isIntersecting: ', list[0].isIntersecting);
                    console.log('list: ', list);
                    let height = Math.floor(Math.random() * 50 + 400)
                    let dom = document.createElement('span');
                    dom.style.height = `${height}px`;
                    let height1 = id1.clientHeight;
                    let height2 = id2.clientHeight;
                    if (height1 <= height2) {
                        id1.appendChild(dom);
                    } else {
                        id2.appendChild(dom);
                    }
                }
            })
            intersectionObserver.observe(id3);
            time = setInterval(function (x) {}, 2000)

        </script>

    </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值