js瀑布流可加载更多

  1. 第一个是html文件

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <link rel="stylesheet" href="../css/css1.css">
    <script src="瀑布流js.js">script>
head>
<body>

    <div id="container">
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/1.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/2.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/3.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/4.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/5.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/6.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/7.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/8.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/9.jpg" alt="">
            div>
        div>
        <div class="box">
            <div class="box_img">
                <img src="../Image/10.jpg" alt="">
            div>
        div>
    div>
body>
html>
  1. 第二个是css文件
*{
    margin: 0px;
    padding: 0px;
}
img{
    width: 150px;
    height: aotu;
}
.box_img{
    padding: 5px;
    border: 1px solid #cccccc;
    box-shadow: 0 0 5px #ccc;
    border-radius: 5px;
}

#container{
    position: relative;
}
.box{
    padding: 5px;
    float: left;
}

  1. 第三个是js文件
window.onload = function () {
    imgLocation("container","box");
    var imgData = {"data":[{"src":"../Image/1.jpg"},{"src":"../Image/2.jpg"},{"src":"../Image/3.jpg"},
            {"src":"../Image/4.jpg"},{"src":"../Image/5.jpg"},{"src":"../Image/6.jpg"},
            {"src":"../Image/7.jpg"},{"src":"../Image/8.jpg"},{"src":"../Image/9.jpg"},{"src":"../Image/10.jpg"}]}
    window.onscroll = function () {
        if (checkFlag()){
            var cparent = document.getElementById("container");
            for (var i = 0;i < imgData.data.length;i++){
                //创建一个div元素 box
                var ccontent = document.createElement("div");
                ccontent.className = "box";
                //放在container里面
                cparent.appendChild(ccontent);
                //创建一个div元素 box_img 放在 box 里面
                var boxImg = document.createElement("div");
                boxImg.className = "box_img";
                //将box_img放在box 里面
                ccontent.appendChild(boxImg);
                //创建图片节点
                var img = document.createElement("img");
                //图片的来源是 上面定义的数组内的
                img.src = imgData.data[i].src;
                //将图片添加在box_img 里面
                boxImg.appendChild(img);
            }
            imgLocation("container","box");
        }
    }
}
function checkFlag() {
    var cparent = document.getElementById("container");
    var cconent = getChildElement(cparent,"box");
    var lastContentHeight = cconent[cconent.length - 1].offsetTop;
    //滚动条滚动的时候,值一点一点的在增加 获取滚动条的位置
    var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
    //浏览器窗口的大小
    var pageHeight = document.documentElement.clientHeight||document.body.offsetHeight;
    //如果滚动条的位置加上 页面的高度 大于 最后一张图片的位置高度
    if (lastContentHeight < scrollTop + pageHeight){
        return true;
    }

    //console.log(lastContentHeight+":"+scrollTop+":"+pageHeight);
}
function imgLocation(parent,content) {
    var cparent = document.getElementById(parent);
    var ccontent = getChildElement(cparent,content);//得到10个name为box的div
    var imgWidth = ccontent[0].offsetWidth;
    //var width = document.documentElement.clientWidth;
    var calc = Math.floor(document.documentElement.clientWidth / imgWidth);
    //以下一行代码 是设置父节点div标签的宽度 一定要注意 width后面有冒号:
    //设置了之后 页面的布局就不会随着我们拉动页面大小 图片位置发生变化了
    cparent.style.cssText = "width:" + (imgWidth * calc) +"px;margin:0 auto";

    var BoxHeightArr = [];
    for (var i = 0;i < ccontent.length;i++){
        //这里ccontent.length == 10 calc == 8
        if(i < calc){
            BoxHeightArr[i] = ccontent[i].offsetHeight;
        }else {
            //得到高度最低的图片的高度
            var minHeight = Math.min.apply(null,BoxHeightArr);
            //得到高度最低的图片的位置索引
            var index = getMinHeightLocation(BoxHeightArr,minHeight);
            //设置位置为绝对定位
            ccontent[i].style.position = "absolute";
            //将其余的图片放入到最短高度的图片下面
            ccontent[i].style.top = minHeight + "px";
            //图片向左居中
            ccontent[i].style.left = ccontent[index].offsetLeft+"px";
            //此时高度最低的图片高度 == 本身高度+加上去的图片的高度
            BoxHeightArr[index] = BoxHeightArr[index] + ccontent[i].offsetHeight;
        }
    }
}
//找到高度最低的图片的索引
function getMinHeightLocation(BoxHeightArr,minHeight) {
    for (var i in BoxHeightArr){
        if (BoxHeightArr[i] == minHeight){
            return i;
        }
    }
}
//得到name为box的节点数组
function getChildElement(parent,content) {
    var contentArr = [];
    //得到所有元素
    var allcantent = parent.getElementsByTagName("*");
    for (var i = 0;i < allcantent.length;i++){
        //如果节点的className == "box"
        if (allcantent[i].className == content){
            //那么元素加入到 contentArr数组里面
            contentArr.push(allcantent[i]);
        }
    }
    return contentArr;
}

你可能感兴趣的:(js瀑布流可加载更多)