瀑布流布局特效

HTML代码部分

    

    

    

    瀑布流布局

    

    

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

        

            

        

    

    

CSS部分

* {

    margin: 0;

    padding: 0;

    border: 0;

}

#bigbox {

    margin: 0 auto;

    position: relative;

    /* width: 1440px; */

}

.box {

    padding: 15px 0 0 15px;

    float: left;

    /* position: absolute; */

}

.pic {

    padding: 10px;

    border: 1px solid #ccc;

}

.pic img {

    width: 165px;

}

img{

    vertical-align: top;

}

JS部分

window.addEventListener('load',function(){

    let bigBox = document.getElementById('bigbox')

    waterFull("bigbox","box");

    let timer = null;

    window.onscroll = function(){

        clearTimeout(timer)

        timer = setTimeout(function(){

            if(ifloadMore("bigbox","box")){

                let dataArr = [     //实际开发过程中本阶段通过ajax从服务器拿到data数据

                    {"src": "img04.jpg"},

                    {"src": "img06.jpg"},

                    {"src": "img08.jpg"},

                    {"src": "img09.jpg"},

                    {"src": "img10.jpg"},

                    {"src": "img12.jpg"},

                    {"src": "img14.jpg"},

                    {"src": "img16.jpg"},

                    {"src": "img18.jpg"}

                ]

                for(let i=0;i

                    let newBox = document.createElement('div');

                    newBox.setAttribute('class','box');

                    bigBox.appendChild(newBox)


                    let newPic = document.createElement('div');

                    newPic.setAttribute('class','pic')

                    newBox.appendChild(newPic)


                    let newImg = document.createElement('img')

                    newImg.src = 'image/'+dataArr[i].src

                    newPic.appendChild(newImg)

                }

                setTimeout(waterFull("bigbox","box"),3000)

            }

        },2000)


    }

    let timer1 = null;

    window.onresize = function(){

        clearTimeout(timer1)

        timer1 = setTimeout(function(){

            waterFull("bigbox","box")

            console.log(1)

        },1000)


    }

})

function waterFull (parent,child) {

    let bigBox = document.getElementById(parent);

    let smallBox = bigBox.getElementsByClassName(child);

    let boxWidth = smallBox[0].offsetWidth;

    let cliWidth = document.documentElement.clientWidth;

    let cols = parseInt(cliWidth/boxWidth)

    bigBox.style.width = cols*boxWidth +'px';

    let heightArr = [];

    for(let i=0;i

        boxHeight = smallBox[i].offsetHeight;

        if(i

            heightArr.push(boxHeight);

            smallBox[i].style = ''

        }else{

            minBoxHeight = Math.min(...heightArr);

            minBoxIndex = getMinBoxIndex(heightArr,minBoxHeight);

            smallBox[i].style.position = 'absolute';

            smallBox[i].style.left = minBoxIndex*boxWidth+ 'px'

            smallBox[i].style.top = minBoxHeight + 'px'

            heightArr[minBoxIndex] += boxHeight

        }

    }

}

function ifloadMore (parent,child){

    let bigBox = document.getElementById(parent);

    let smallBoxs = bigBox.getElementsByClassName(child);

    let lastBox = smallBoxs[smallBoxs.length-1];

    let lastBoxDis = lastBox.offsetHeight*0.5 + lastBox.offsetTop;

    let cliHeight = document.documentElement.clientHeight;

    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

    return (scrollTop+cliHeight) >= lastBoxDis

}

function getMinBoxIndex (arr,val){

    for(let i=0;i

        if(arr[i]==val){

            return i

        }

    }

}

你可能感兴趣的:(瀑布流布局特效)