码迷,mamicode.com
首页 > 其他好文 > 详细

简单实现瀑布流效果

时间:2016-01-27 12:59:07      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

PUBUmodel.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="css/pubu.css">
    <script src="JS/PU.js" type="text/javascript" charset="utf-8" defer></script>
</head>
<body>
    <div id="container">
    <div class="box"><div class="img_box"><img src="images/imagess/ima1.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima2.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima3.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima4.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima5.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima6.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima7.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima8.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima9.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima10.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima1.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima2.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima3.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima4.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima5.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima6.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima7.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima8.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima9.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima10.jpg" alt=" "></div></div>
     <div class="box"><div class="img_box"><img src="images/imagess/ima2.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima3.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima4.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima5.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima6.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima7.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima8.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima9.jpg" alt=" "></div></div>
    <div class="box"><div class="img_box"><img src="images/imagess/ima10.jpg" alt=" "></div></div>
    </div>
</body>
</html>

---------------------------------------------分割线看什么看-------------------------------------------------------------

PU.js

window.onload=function () {
    imgLocation("container", "box");
    var imgdata={"data":[{"src":"ima1.jpg"},{"src":"ima2.jpg"},{"src":"ima3.jpg"},{"src":"ima4.jpg"},{"src":"ima5.jpg"},{"src":"ima6.jpg"},{"src":"ima7.jpg"},{"src":"ima8.jpg"}]};
    window.onscroll=function(){
        if(check()){
            var len=imgdata.data.length
            for(var i=0;i<len;i++){
                var div=document.createElement("div");
                div.className="box";
                var imgdiv=document.createElement("div");
                imgdiv.className="img_box";
                div.appendChild(imgdiv);
                var img=document.createElement("img");
                img.src="images/imagess/"+imgdata.data[i].src;
                imgdiv.appendChild(img);
                var parent=getParent("container");
                parent.appendChild(div);
                imgLocation("container", "box");
            }
        }
    }
}
    function check(){
        var cparent=getParent("container");
        var content=getAllcontent(cparent,"box");
        var scrollTop=document.documentElement.scrollTop;
        var clientHeight=document.documentElement.clientHeight;
        var lastImgTop=content[content.length-1].offsetTop;
        if(scrollTop+clientHeight>=lastImgTop){
            return true;
        }
    }
function getParent(parent){
    return document.getElementById(parent);
}
function imgLocation(parent,content) {
    var cparent=getParent(parent);
    var ccontent=getAllcontent(cparent, "box");
    var imgWidth=ccontent[0].offsetWidth;
    var cWidth=document.documentElement.clientWidth||document.body.clientWidth;
    var num=Math.floor(cWidth/imgWidth);
    cparent.style.cssText="width:"+num*imgWidth+"px;margin:0px auto;";
    var len=ccontent.length;
    var ArrHeight=[];
    for(var i=0;i<len;i++){
        if(i<num){
            ArrHeight[i]=ccontent[i].offsetHeight;
        }
        else{
            var minHeight=Math.min.apply(null,ArrHeight);
            var minloca=getMinHeightLocation(ArrHeight,minHeight);
            var offleft=ccontent[minloca].offsetLeft;
            ccontent[i].style.position="absolute";
            ccontent[i].style.left=offleft+"px";
            ccontent[i].style.top=minHeight+"px";
            ArrHeight[minloca]+=ccontent[i].offsetHeight;
        }
    }
}
function getMinHeightLocation(content,minHeight){
    var num=content.length;
    for(var i=0;i<num;i++){
        if(content[i]==minHeight){
            return i;
        }
    }
}
function getAllcontent(cparent,box){
    var Allcontent=[];
    var All=cparent.getElementsByTagName(‘*‘);
    var len=All.length;
    for(var i=0;i<len;i++){
        if(All[i].className==box){
            Allcontent.push(All[i]);
        }
    }
    return Allcontent;
}

---------------------------------------------分割线看什么看-------------------------------------------------------------

pubu.css

*{
    margin: 0;
    padding: 0;
}
#container{
    position: relative;
}
.box{
    float: left;
    padding: 5px;
}
.img_box{
    padding: 5px;
    border: 1px solid #ccc;
    box-shadow: 0 0 5px #ccc;
    border-radius: 5px;
}
.img_box img{
    width: 150px;
    height: auto;
}

 

效果如图:

技术分享

简单实现瀑布流效果

标签:

原文地址:http://www.cnblogs.com/xianhaiyuan/p/5162744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!