无限瀑布流 JS原生代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/initialize.css">
    <style>
        .out {
            position: relative;
            margin: 0 auto;
        }

        .in {
            float: left;
        }

        .img {
            margin: 10px;
            padding: 10px;
            border-radius: 10px;
            border: 1px solid #666;
            box-shadow: 0 0 5px #ff7ba5;

        }
    </style>
</head>
<body>
<div class="out">
    <div class="in">
        <div class="img"><img src="img/i1.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i2.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i3.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i4.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i5.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i6.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i7.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i8.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i9.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i10.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i11.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i12.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i13.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i14.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i15.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i3.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i4.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i5.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i6.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i7.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i8.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i9.jpg" alt=""></div>
    </div>
    <div class="in">
        <div class="img"><img src="img/i10.jpg" alt=""></div>
    </div>
</div>

<script>
    window.onload = function () {
        var data = '{"data":[{"src":"i1.jpg"},{"src":"i2.jpg"},{"src":"i3.jpg"},{"src":"i4.jpg"},{"src":"i5.jpg"},{"src":"i6.jpg"},{"src":"i7.jpg"},{"src":"i8.jpg"},{"src":"i9.jpg"},{"src":"i10.jpg"},{"src":"i11.jpg"},{"src":"i12.jpg"},{"src":"i13.jpg"},{"src":"i14.jpg"},{"src":"i15.jpg"}]}';
        var out = document.getElementsByClassName('out')[0];
        var inName = document.getElementsByClassName('in');
        function pbl() {
            var arr = [];
            var winWid = document.documentElement.clientWidth;
            var imgWid = inName[0].offsetWidth;
            num = Math.floor(winWid / imgWid);
            if(num<1) return;
            out.style.width = num * imgWid + 'px';
            for (var i = 0; i < inName.length; i++) {
                if (i < num) {
                    arr.push(inName[i].offsetHeight);
                    inName[i].style.position = 'static';
                } else {
                    inName[i].style.position = 'absolute';
                    var minArr = Math.min.apply(null, arr);
                    var index = arr.indexOf(minArr);
                    inName[i].style.top = minArr + 'px';
                    arr[index] += inName[i].offsetHeight;
                    inName[i].style.left = inName[index].offsetLeft + 'px';
                }
            }
        }
        pbl();
        window.onscroll = function () {
            var widH = document.documentElement.clientHeight; //获取时候的高度
            var tTop = inName[inName.length - 1].offsetTop; //获取最后一张图片到顶部的距离
            var hdH = document.documentElement.scrollTop||document.body.scrollTop; //获取滑动的高度
            if(tTop<widH+hdH){
                var jieGuo = JSON.parse(data);
                for(var i=0;i<jieGuo.data.length;i++){
                    var div1 = document.createElement('div');
                    div1.className = 'in';
                    var div2 = document.createElement('div');
                    div2.className = 'img';
                    var img = new Image;
                    img.src = 'img/'+jieGuo.data[i].src;
                    div2.appendChild(img);
                    div1.appendChild(div2);
                    out.appendChild(div1);
                }
                pbl();
            }
        };
        window.onresize = function () {
            pbl();
        }
    };



</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值