实现瀑布流布局效果两种方式

使用原生JavaScript实现瀑布流


// index.html


<html>
<head>
    <title>瀑布流布局title>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script type="text/javascript" src="js/script.js">script>
head>
<body>
    <div id="main">
        <div class="box">
            <div class="pic">
                <img src="images/0.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/1.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/2.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/3.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/4.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/5.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/6.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/7.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/8.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/9.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/10.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/0.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/1.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/2.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/3.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/4.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/5.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/6.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/7.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/8.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/9.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/10.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/0.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/1.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/2.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/3.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/4.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/5.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/6.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/7.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/8.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/9.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/10.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/0.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/1.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/2.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/3.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/4.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/5.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/6.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/7.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/8.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/9.jpg" />
            div>
        div>
        <div class="box">
            <div class="pic">
                <img src="images/10.jpg" />
            div>
        div>
    div>
body>
html>
// script.js

window.onload = function(){
    waterfall("main","box");
    var dataInt = {"data":[{"src":"92.jpg"},{"src":"93.jpg"},{"src":"94.jpg"},{"src":"95.jpg"}]}
    window.onscroll = function(){
        if(checkScrollSlide()){
            var oParent = document.getElementById("main");
            // 将数据块渲染后当前页面的尾部
            for(var i = 0; i < dataInt.data.length; i++){
                var oBox = document.createElement("div");
                oBox.className = "box";
                oParent.appendChild(oBox);
                var oPic = document.createElement("div");
                oPic.className = "pic";
                oBox.appendChild(oPic);
                var oImg = document.createElement("img");
                oImg.src = "images/" + dataInt.data[i].src;
                oPic.appendChild(oImg);
            }
            waterfall("main","box");
        }
    }
}

function waterfall(parent,box){
    // 将main下的所有class为box的元素取出来
    var oParent = document.getElementById(parent);
    var oBoxes = getByClass(oParent,box);
    // 计算整个页面显示的列数(页面宽/box的宽)
    var oBoxWidth = oBoxes[0].offsetWidth; // 等宽不等高
    var cols = Math.floor(document.documentElement.clientWidth/oBoxWidth);
    // 设置main的宽度
    oParent.style.cssText = "width:" + oBoxWidth * cols + "px;margin:0 auto;";
    // 存放第一列高度的数组
    var heightArr = [];
    for(var i = 0; i < oBoxes.length; i++){
        if(i < cols){
            heightArr.push(oBoxes[i].offsetHeight);
        }else{
            var minHeight = Math.min.apply(null,heightArr);
            var index = getMinHeightIndex(heightArr,minHeight); // 获取最小高度的索引
            oBoxes[i].style.position = "absolute";
            oBoxes[i].style.top = minHeight + "px";
            oBoxes[i].style.left = oBoxWidth * index + "px";
            heightArr[index] = minHeight + oBoxes[i].offsetHeight;  // 改变当前列的高度
        }
    }
}

// 根据class获取元素
function getByClass(parent,className){
    var boxArr = new Array(), // 用来存储获取到的所有class为box的元素
        oElements = parent.getElementsByTagName("*");

    for(var i = 0; i < oElements.length; i++){
        if(oElements[i].className == className){
            boxArr.push(oElements[i]);
        }
    }

    return boxArr;
}

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

// 检测是否具备了滚条加载数据块的条件
function checkScrollSlide(){
    var oParent = document.getElementById("main");
    var oBoxes = getByClass(oParent,"box");
    var lastBoxHeight = oBoxes[oBoxes.length - 1].offsetTop + Math.floor(oBoxes[oBoxes.length - 1].offsetHeight / 2);
    var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    var height = document.body.clientWidth || document.documentElement.clientHeight;

    return (lastBoxHeight < scrollTop + height) ? true : false;
}
// style.css

*{margin: 0; padding: 0;}

#main{position: relative;}

.box{padding: 15px 0 0 15px;
     float: left;}

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

.pic img{width:165px;
         height: auto;}

使用JQuery实现瀑布流 ##(注意导入jquery.js)

// index.html


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/jquery-1.8.3.min.js"/>script>
<script type="text/javascript" src="js/waterfall.js"/>script>

<title>title>
<style type="text/css">
    *{padding: 0;margin:0;}
    #main{
        position: relative;
    }
    .box{
        padding: 15px 0 0 15px;
        float:left;
    }
    .pic{
        padding: 10px;
        border:1px solid #ccc;
        box-shadow: 0 0 6px #ccc;
        border-radius: 5px;
    }
    .pic img{
        width:162px;
        height:auto;
    }
style>
head>
<body>
<div id="main">
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
    <div class="box">
        <div class="pic">
            <img src="./images/1.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/2.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/3.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/4.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/5.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/6.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/7.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/8.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/9.jpg"/>
        div>
    div>
     <div class="box">
        <div class="pic">
            <img src="./images/10.jpg"/>
        div>
    div>
div>
body>
html>
// waterfall.js

$(window).on("load",function(){
    waterfall();
    var dataInt = {"data":[{"src":"92.jpg"},{"src":"93.jpg"},{"src":"94.jpg"},{"src":"95.jpg"}]}
    $(window).on("scroll",function(){
        if(checkScrollSlide()){

            $.each(dataInt.data,function(key,value){
                var oBox = $("
").addClass("box").appendTo($("#main")); var oPic = $("
").addClass("pic").appendTo($(oBox)); $("").attr("src","images/" + $(value).attr("src")).appendTo($(oPic)); }) waterfall(); } }) }) function waterfall(){ var $boxes = $("#main>div"); var w = $boxes.eq(0).outerWidth(); var cols = Math.floor($(window).width() / w); $("#main").width(w * cols).css("margin","0 auto"); var heightArr = []; $boxes.each(function(index,value){ var height = $boxes.eq(index).outerHeight(); if(index < cols){ heightArr[index] = height; }else{ var minHeight = Math.min.apply(null,heightArr); var minHeightIndex = $.inArray(minHeight,heightArr); // 使用$符号把document对象value转换成jquery对象 $(value).css({ "position":"absolute", "top":minHeight+"px", "left":minHeightIndex * w + "px" }) heightArr[minHeightIndex] += $boxes.eq(index).outerHeight(); } }) } function checkScrollSlide(){ var $lastBox = $("#main>div").last(); var lastBoxDis = $lastBox.offset().top + Math.floor($lastBox.outerHeight() / 2); // 距离页面顶部的距离 var scrollTop = $(window).scrollTop(); var documentHeight = $(window).height(); return (lastBoxDis < scrollTop + documentHeight) ? true : false; }

运行效果如下:

实现瀑布流布局效果两种方式_第1张图片

你可能感兴趣的:(编程开发,Front)