javaScript基础学习(6)(瀑布流例子)

html:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>瀑布流</title>
    <link rel="stylesheet" href="../css/pubu.css" type="text/css">
    <script src="pubu.js" type="text/javascript"></script>
</head>
<body>
    <div id="container">
        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img" >
                <img src="../image/image.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image2.jpg">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>

        <div class="box">
            <div class="box_img">
                <img src="../image/image3.png">
            </div>
        </div>
    </div>
</body>
</html>

css:

*{
    margin: 0px;
    padding: 0px;
}

#container{
    position: relative;
}

.box{
    padding: 5px;
    float: left;
}

.box_img {
    padding: 5px;
    border: 1px solid #ccc;
    box-shadow:  0 0 5px #ccc;
    border-radius: 5px;
}

.box_img img{
    width: 150px;
    height: auto;
}

js:


window.onload = function(){

    imgLocation('container',"box");

    var imgData = {"data":[{"src":"image.png"},{"src":"image2.jpg"},{"src":"image3.png"}]};

    window.onscroll = function(){
        loadImage(imgData);
    };
    window.onresize = function(){
        imgLocation('container',"box");
    };
}

function loadImage(imgData){
    if(checkFlag()){
        var cparent = document.getElementById("container");
        for(var i=0;i<imgData.data.length;i++) {

            var ccontent = document.createElement("div");
            ccontent.className = "box";
            cparent.appendChild(ccontent);
            var boximg = document.createElement("div");
            boximg.className = "box_img";
            ccontent.appendChild(boximg);
            var img = document.createElement("img");
            img.src = "../image/"+imgData.data[i].src;
            boximg.appendChild(img);
        }
        imgLocation('container',"box");
    }
}

function checkFlag(){
    var cparent = document.getElementById("container");
    var ccontent = getChildElement(cparent,"box");
    var lastczontentHeight = ccontent[ccontent.length-1].offsetTop;
    var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
    var pageHeight = document.documentElement.clientHeight || document.body.clientHeight;
    if(lastczontentHeight<scrollTop+pageHeight){
        return true;
    }
}

//处理图片位置
function imgLocation(parent,content){
    //将parent下所有的content都取出
    var cparent = document.getElementById(parent);
    var ccontent = getChildElement(cparent,content);
    //得到图片的宽度
    var imgWidth = ccontent[0].offsetWidth;
    var cols = Math.floor(document.documentElement.clientWidth / imgWidth);
    //设置父盒子的样式
    cparent.style.cssText = "width:"+imgWidth*cols+"px;margin:0 auto;";

    //获取最短的图片
    var BoxHeightArr = [];
    for(var i=0;i<ccontent.length;i++){
        if(i<cols){
            BoxHeightArr[i] = ccontent[i].offsetHeight;
        }else{
            //获取最小高度
            var minHeight = Math.min.apply(null,BoxHeightArr);
            var minIndex  = getminHeightLocation(BoxHeightArr,minHeight);
            ccontent[i].style.position = "absolute";
            ccontent[i].style.top = minHeight + "px";
            ccontent[i].style.left = ccontent[minIndex].offsetLeft + "px";
            BoxHeightArr[minIndex] =  BoxHeightArr[minIndex]+ccontent[i].offsetHeight;
        }
    }
}

//获取最小高度的下标
function getminHeightLocation(boxHeightArr,minHeight){
    for(var i in boxHeightArr){
        if(boxHeightArr[i] == minHeight){
             return i;
        }
    }
}

//获取所有父盒子的子元素
function getChildElement(parent,content){
    var contentArr = [];
    var allcontent = parent.getElementsByTagName("*");
    for(var i = 0;i<allcontent.length;i++){
        if(allcontent[i].className == content){
            contentArr.push(allcontent[i]);
        }
    }
    return contentArr;
}

效果图:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值