![](../waterFall/pic/0.jpg)
![](../waterFall/pic/1.jpg)
![](../waterFall/pic/2.jpg)
![](../waterFall/pic/3.jpg)
![](../waterFall/pic/4.jpg)
![](../waterFall/pic/5.jpg)
![](../waterFall/pic/6.jpg)
![](../waterFall/pic/7.jpg)
![](../waterFall/pic/8.jpg)
![](../waterFall/pic/9.jpg)
![](../waterFall/pic/10.jpg)
![](../waterFall/pic/11.jpg)
![](../waterFall/pic/12.jpg)
![](../waterFall/pic/13.jpg)
![](../waterFall/pic/14.jpg)
![](../waterFall/pic/15.jpg)
![](../waterFall/pic/16.jpg)
![](../waterFall/pic/17.jpg)
![](../waterFall/pic/18.jpg)
![](../waterFall/pic/19.jpg)
![](../waterFall/pic/20.jpg)
![](../waterFall/pic/21.jpg)
![](../waterFall/pic/22.jpg)
![](../waterFall/pic/23.jpg)
![](../waterFall/pic/24.jpg)
![](../waterFall/pic/25.jpg)
![](../waterFall/pic/26.jpg)
![](../waterFall/pic/27.jpg)
![](../waterFall/pic/28.jpg)
![](../waterFall/pic/29.jpg)
![](../waterFall/pic/30.jpg)
![](../waterFall/pic/31.jpg)
![](../waterFall/pic/32.jpg)
![](../waterFall/pic/33.jpg)
![](../waterFall/pic/34.jpg)
![](../waterFall/pic/35.jpg)
![](../waterFall/pic/36.jpg)
![](../waterFall/pic/37.jpg)
![](../waterFall/pic/38.jpg)
![](../waterFall/pic/39.jpg)
![](../waterFall/pic/40.jpg)
![](../waterFall/pic/41.jpg)
![](../waterFall/pic/42.jpg)
![](../waterFall/pic/43.jpg)
The examples in this article share with you the js waterfall flow layout learning materials for your reference. The specific content is as follows
Features: equal width and unequal height.
Implementation method: Javascript/Jquery/CSS3 multi-column layout.
Sample website: Huaban.com -->Category
1. JS to implement waterfall flow
index.html: Page structure
layout. css: page element style
*{ pdding:0; margin:0; } div#main{ position: relative; } div.box{ padding:15px 0 0 15px; float: left; } div.pic{ padding:10px; border:1px solid #ccc; border-radius:5px; box-shadow: 0 0 5px #ccc; } .pic img{ height:auto; width:165px; }
waterfall.js
window.onload=function(){ waterFall('main','box'); //模拟后台相应数据json var dataInt={ "data": [ {"src":"0.jpg"}, {"src":"1.jpg"}, {"src":"2.jpg"}, {"src":"3.jpg"}, {"src":"4.jpg"}, {"src":"5.jpg"}, {"src":"6.jpg"} ] } window.onscroll=function(){ if(checkScrollSlide){ //将数据块渲染到当前页面的尾部 var oParent=document.getElementById("main"); for(var i=0;i Copy after login
2. JQuery
$( window ).on( "load", function(){ waterfall('main','pin'); var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]}; window.onscroll=function(){ if(checkscrollside()){ $.each( dataInt.data, function( index, value ){ var $oPin = $('').addClass('pin').appendTo( $( "#main" ) ); var $oBox = $('').addClass('box').appendTo( $oPin ); $('').attr('src','./images/' + $( value).attr( 'src') ).appendTo($oBox); }); waterfall(); }; } }); /* parend 父级id pin 元素id */ function waterfall(parent,pin){ var $aPin = $( "#main>div" ); var iPinW = $aPin.eq( 0 ).width();// 一个块框pin的宽 var num = Math.floor( $( window ).width() / iPinW );//每行中能容纳的pin个数【窗口宽度除以一个块框宽度】 //oParent.style.cssText='width:'+iPinW*num+'px;ma rgin:0 auto;';//设置父级居中样式:定宽+自动水平外边距 $( "#main" ).css({ 'width:' : iPinW * num, 'margin': '0 auto' }); var pinHArr=[];//用于存储 每列中的所有块框相加的高度。 $aPin.each( function( index, value ){ var pinH = $aPin.eq( index ).height(); if( index < num ){ pinHArr[ index ] = pinH; //第一行中的num个块框pin 先添加进数组pinHArr }else{ var minH = Math.min.apply( null, pinHArr );//数组pinHArr中的最小值minH var minHIndex = $.inArray( minH, pinHArr ); $( value ).css({ 'position': 'absolute', 'top': minH + 15, 'left': $aPin.eq( minHIndex ).position().left }); //数组 最小高元素的高 + 添加上的aPin[i]块框高 pinHArr[ minHIndex ] += $aPin.eq( index ).height() + 15;//更新添加了块框后的列高 } }); } function checkscrollside(){ var $aPin = $( "#main>div" ); var lastPinH = $aPin.last().get(0).offsetTop + Math.floor($aPin.last().height()/2);//创建【触发添加块框函数waterfall()】的高度:最后一个块框的距离网页顶部+自身高的一半(实现未滚到底就开始加载) var scrollTop = $( window ).scrollTop()//注意解决兼容性 var documentH = $( document ).height();//页面高度 return (lastPinH < scrollTop + documentH ) ? true : false;//到达指定高度后 返回true,触发waterfall()函数 }
Copy after login3. CSS multi-column layout
.container{ -webkit-column-width:160px; -moz-column-width:160px; -webkit-column-gap:5px; -moz-column-gap:5px; } /*数据块 砖块*/ .container div{width:160px; margin:4px 0;}Copy after login[css3 and js implementation Comparison of methods】
--css3 method--
1: No calculation required, the browser automatically calculates, just set 1 column width, high performance
2: Column width changes with the browser width size Change, the user experience is not good;
3: The pictures are sorted in vertical order, disrupting the order of picture display
4. Picture loading still requires js
--js method--
js implemented waterfall Streaming does not have the above shortcomings, but the performance is relatively poor!The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to in-depth study of js waterfall flow layout, please pay attention to the PHP Chinese website!