原生js--图片的瀑布流

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>图片瀑布流1</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        body{
            background-color: cornsilk;
        }
        .block{
            position: relative;
            margin:auto;
            width:870px;
            height: auto;
            background-color: white;
        }
        .divlist{
            position: absolute;
            width:100px;
            height:auto;
        }
        .divlist>img{
            width:100%;
            height:auto;
            box-shadow: 0 0 8px black;
        }
    </style>
</head>
<body>
<script src="./js/图片瀑布流1.js"></script>
</body>
</html>
/**
 * Created by Administrator on 2019/5/9.
 */
var block=document.createElement("div");
block.className="block";
document.body.appendChild(block);

var count=8;
var image=["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"];
var startimage=48;
var bgheight=0;
var imager=[];
var pageimage=16;
var pageindex=0;
DivCreat(startimage);
window.onload=function(){
    offset(pageindex);
};
window.onscroll=function(){
    if(window.innerHeight+dealfirefoxscroll()>document.body.clientHeight-50){
        pageindex=startimage;
        startimage+=pageimage;
        DivCreat(pageimage);
        offset(pageindex);
    }
};
function dealfirefoxscroll(){
    return window.pageYOffset||document.documentElement.scrollTop;
}
function DivCreat(number){
    for(i=0;i<number;i++){
        var divlist=document.createElement("div");
        var img=document.createElement("img");
        divlist.className="divlist";
        block.appendChild(divlist);
        divlist.appendChild(img);
        img.src="./img/"+image[Math.floor(Math.random()*image.length)];
        imager.push(divlist);
        }
}
function offset(index) {
    for (i = index; i < imager.length; i++) {
        imager[i].style.left = ((i % count) * (100 + 10)) + "px";
        imager[i].style.top = (i - count) < 0 ? 0 : ((imager[i - count].offsetHeight + imager[i - count].offsetTop) + 10) + "px";
        bgheight=bgheight-(imager[i].offsetHeight+imager[i].offsetTop)>0?bgheight:(imager[i].offsetHeight+imager[i].offsetTop);
        block.style.height=bgheight+"px";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值