Home  >  Article  >  Web Front-end  >  Learn more about js waterfall flow layout

Learn more about js waterfall flow layout

高洛峰
高洛峰Original
2017-01-03 10:36:001083browse

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

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()函数 }

3. 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;}

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]