JQuery 图片瀑布流

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
        }

        html, body {
            background-color: #535353;
        }

        .block {
            position: relative;
            width: 840px;
            margin: 0 auto;
            border: 1px solid white;
        }

        .bchild {
            position: absolute;
            float: left;
            width: 200px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 0 10px silver;
            margin: 0 5px;
        }

        .imglist {
            width: 180px;
            margin: 10px 10px;
        }
    </style>
</head>
<body>
<div class="block">

</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
    var list = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "13.jpg", "14.jpg", "15.jpg"];
    showImage(list);
    $(document).bind("scroll", function () {
        if ($(window).height() + $(this).scrollTop() >= $(document).height() - 20) {
            var newlist = [list[Math.floor(Math.random() * list.length)], list[Math.floor(Math.random() * list.length)], list[Math.floor(Math.random() * list.length)], list[Math.floor(Math.random() * list.length)]];
            showImage(newlist);
        }
    });
    function showImage(obj) {
        //创建元素的
        obj.forEach(function (res, index) {
            var bo = $("<div class='bchild'></div>");
            var img = $("<img class='imglist'>");
            img.attr("src", "./img/" + res);
            bo.append(img);
            $(".block").append(bo);
        });
        //遍历了所有  容器
        $(".bchild").each(function (index) {
            if (index < 4) {
                $(".bchild").eq(index).css("top", 10);
            }
            else {
                $(".bchild").eq(index).css({
                    "top": $(".bchild").eq(index - 4)[0].offsetHeight + $(".bchild").eq(index - 4)[0].offsetTop + 10
                });
            }
            $(".bchild").eq(index).css("left", index % 4 * 210);
        });
        $(".block").css("height", $(document).height());
    }
</script>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值