一、JS實現瀑布流 index.html:頁面結構 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>瀑布流佈局</title> 6 <link rel="stylesheet" href= ...
- 特點:等寬不等高。
- 實現方式:Javascript/Jquery/CSS3多欄佈局。
- 樣例網站:花瓣網-->分類(http://huaban.com/)
一、JS實現瀑布流
index.html:頁面結構
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>瀑布流佈局</title> 6 <link rel="stylesheet" href="styles/layout.css"> 7 </head> 8 <body> 9 <div id="main"> 10 <div class="box"><div class="pic"><img src="../waterFall/pic/0.jpg" alt=""></div></div> 11 <div class="box"><div class="pic"><img src="../waterFall/pic/1.jpg" alt=""></div></div> 12 <div class="box"><div class="pic"><img src="../waterFall/pic/2.jpg" alt=""></div></div> 13 <div class="box"><div class="pic"><img src="../waterFall/pic/3.jpg" alt=""></div></div> 14 <div class="box"><div class="pic"><img src="../waterFall/pic/4.jpg" alt=""></div></div> 15 <div class="box"><div class="pic"><img src="../waterFall/pic/5.jpg" alt=""></div></div> 16 <div class="box"><div class="pic"><img src="../waterFall/pic/6.jpg" alt=""></div></div> 17 <div class="box"><div class="pic"><img src="../waterFall/pic/7.jpg" alt=""></div></div> 18 <div class="box"><div class="pic"><img src="../waterFall/pic/8.jpg" alt=""></div></div> 19 <div class="box"><div class="pic"><img src="../waterFall/pic/9.jpg" alt=""></div></div> 20 <div class="box"><div class="pic"><img src="../waterFall/pic/10.jpg" alt=""></div></div> 21 <div class="box"><div class="pic"><img src="../waterFall/pic/11.jpg" alt=""></div></div> 22 <div class="box"><div class="pic"><img src="../waterFall/pic/12.jpg" alt=""></div></div> 23 <div class="box"><div class="pic"><img src="../waterFall/pic/13.jpg" alt=""></div></div> 24 <div class="box"><div class="pic"><img src="../waterFall/pic/14.jpg" alt=""></div></div> 25 <div class="box"><div class="pic"><img src="../waterFall/pic/15.jpg" alt=""></div></div> 26 <div class="box"><div class="pic"><img src="../waterFall/pic/16.jpg" alt=""></div></div> 27 <div class="box"><div class="pic"><img src="../waterFall/pic/17.jpg" alt=""></div></div> 28 <div class="box"><div class="pic"><img src="../waterFall/pic/18.jpg" alt=""></div></div> 29 <div class="box"><div class="pic"><img src="../waterFall/pic/19.jpg" alt=""></div></div> 30 <div class="box"><div class="pic"><img src="../waterFall/pic/20.jpg" alt=""></div></div> 31 <div class="box"><div class="pic"><img src="../waterFall/pic/21.jpg" alt=""></div></div> 32 <div class="box"><div class="pic"><img src="../waterFall/pic/22.jpg" alt=""></div></div> 33 <div class="box"><div class="pic"><img src="../waterFall/pic/23.jpg" alt=""></div></div> 34 <div class="box"><div class="pic"><img src="../waterFall/pic/24.jpg" alt=""></div></div> 35 <div class="box"><div class="pic"><img src="../waterFall/pic/25.jpg" alt=""></div></div> 36 <div class="box"><div class="pic"><img src="../waterFall/pic/26.jpg" alt=""></div></div> 37 <div class="box"><div class="pic"><img src="../waterFall/pic/27.jpg" alt=""></div></div> 38 <div class="box"><div class="pic"><img src="../waterFall/pic/28.jpg" alt=""></div></div> 39 <div class="box"><div class="pic"><img src="../waterFall/pic/29.jpg" alt=""></div></div> 40 <div class="box"><div class="pic"><img src="../waterFall/pic/30.jpg" alt=""></div></div> 41 <div class="box"><div class="pic"><img src="../waterFall/pic/31.jpg" alt=""></div></div> 42 <div class="box"><div class="pic"><img src="../waterFall/pic/32.jpg" alt=""></div></div> 43 <div class="box"><div class="pic"><img src="../waterFall/pic/33.jpg" alt=""></div></div> 44 <div class="box"><div class="pic"><img src="../waterFall/pic/34.jpg" alt=""></div></div> 45 <div class="box"><div class="pic"><img src="../waterFall/pic/35.jpg" alt=""></div></div> 46 <div class="box"><div class="pic"><img src="../waterFall/pic/36.jpg" alt=""></div></div> 47 <div class="box"><div class="pic"><img src="../waterFall/pic/37.jpg" alt=""></div></div> 48 <div class="box"><div class="pic"><img src="../waterFall/pic/38.jpg" alt=""></div></div> 49 <div class="box"><div class="pic"><img src="../waterFall/pic/39.jpg" alt=""></div></div> 50 <div class="box"><div class="pic"><img src="../waterFall/pic/40.jpg" alt=""></div></div> 51 <div class="box"><div class="pic"><img src="../waterFall/pic/41.jpg" alt=""></div></div> 52 <div class="box"><div class="pic"><img src="../waterFall/pic/42.jpg" alt=""></div></div> 53 <div class="box"><div class="pic"><img src="../waterFall/pic/43.jpg" alt=""></div></div> 54 </div> 55 <script src="scripts/waterfall.js"></script> 56 </body> 57 </html>View Code
layout.css:頁面元素樣式
1 *{ 2 pdding:0; 3 margin:0; 4 } 5 div#main{ 6 position: relative; 7 } 8 div.box{ 9 padding:15px 0 0 15px; 10 float: left; 11 } 12 div.pic{ 13 padding:10px; 14 border:1px solid #ccc; 15 border-radius:5px; 16 box-shadow: 0 0 5px #ccc; 17 } 18 .pic img{ 19 height:auto; 20 width:165px; 21 }View Code
waterfall.js
1 window.onload=function(){ 2 waterFall('main','box'); 3 //模擬後臺相應數據json 4 var dataInt={ 5 "data": 6 [ 7 {"src":"0.jpg"}, 8 {"src":"1.jpg"}, 9 {"src":"2.jpg"}, 10 {"src":"3.jpg"}, 11 {"src":"4.jpg"}, 12 {"src":"5.jpg"}, 13 {"src":"6.jpg"} 14 ] 15 } 16 window.onscroll=function(){ 17 if(checkScrollSlide){ 18 //將數據塊渲染到當前頁面的尾部 19 var oParent=document.getElementById("main"); 20 for(var i=0;i<dataInt.data.length;i++){ 21 var oBox=document.createElement("div"); 22 oBox.className="box"; 23 oParent.appendChild(oBox); 24 var oPic=document.createElement("div"); 25 oPic.className="pic"; 26 oBox.appendChild(oPic); 27 var img=document.createElement("img"); 28 img.setAttribute("src",dataInt.data[i]); 29 img.src="pic/"+dataInt.data[i].src; 30 oPic.appendChild(img); 31 } 32 dataInt=null;//清空數據塊,防止無限載入 33 waterFall('main','box');//對頁面新元素進行佈局渲染 34 } 35 } 36 } 37 function waterFall(parent,box){ 38 //將main下的class為box的所有元素取出來 39 var oParent=document.getElementById(parent); 40 var oBoxs=getByClass(oParent,box); 41 console.log(oBoxs.length); 42 //計算整個頁面顯示的列數(頁面寬/box寬) 43 var oBoxW=oBoxs[0].offsetWidth; 44 // console.log(oBoxW); 45 var cols=Math.floor(document.documentElement.clientWidth/oBoxW); 46 // console.log(cols); 47 //設置main的寬 48 oParent.style.cssText="width:"+oBoxW*cols+"px;margin:0 auto;" 49 var hArr=[];//存放每列高度的數組 50 for(var i=0;i<oBoxs.length;i++){ 51 if(i<cols){ 52 hArr.push(oBoxs[i].offsetHeight); 53 }else{ 54 var minH=Math.min.apply(null,hArr);//獲取當前數組最小高度值 55 // console.log(minH); 56 var index=getMinhIndex(hArr,minH);//獲取數組最小高度的索引 57 //console.log(index); 58 oBoxs[i].style.position="absolute";//將之後的圖片依次絕對定位 59 oBoxs[i].style.top=minH+"px"; 60 oBoxs[i].style.left=index*oBoxW+"px";//計算新圖片所在的位置並賦值 61 hArr[index]+=oBoxs[i].offsetHeight;//變化數組列的高度值,因為加上了一張圖片 62 } 63 //console.log(hArr); 64 } 65 66 } 67 //根據class獲取元素 68 function getByClass(parent,clsName){ 69 var boxArr=new Array(),//用來存儲獲取到的所有class為box的元素 70 oElements=parent.getElementsByTagName("*"); 71 for(var i=0;i<oElements.length;i++){ 72 if(oElements[i].className==clsName){ 73 boxArr.push(oElements[i]); 74 } 75 } 76 return boxArr; 77 } 78 function getMinhIndex(arr,val){ 79 for(var i=0;i<arr.length;i++){ 80 if(arr[i]==val){ 81 return i; 82 } 83 } 84 85 } 86 //檢測是否具備滾動條載入數據塊條件 87 function checkScrollSlide(){ 88 var oParent=document.getElementById("main"); 89 var oBoxs=getByClass(oParent,"box"); 90 var lastBoxH=oBoxs[oBoxs.length-1].offsetTop+Math.floor(oBoxs[oBoxs.length-1].offsetHeight/2); 91 var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;//混雜模式和標準模式下的scrollTop獲取 92 //console.log(scrollTop); 93 var height=document.body.clientHeight||document.documentElement.clientHeight;//混雜模式和標準模式下的瀏覽器視窗高度獲取 94 return (lastBoxH<scrollTop+height)?true:false;//檢測最後一個box高度是否小於滾動高度+視窗高度,返回布爾值 95 96 }
二、JQuery
1 $( window ).on( "load", function(){ 2 waterfall('main','pin'); 3 var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]}; 4 window.onscroll=function(){ 5 if(checkscrollside()){ 6 $.each( dataInt.data, function( index, value ){ 7 var $oPin = $('<div>').addClass('pin').appendTo( $( "#main" ) ); 8 var $oBox = $('<div>').addClass('box').appendTo( $oPin ); 9 $('<img>').attr('src','./images/' + $( value).attr( 'src') ).appendTo($oBox); 10 }); 11 waterfall(); 12 }; 13 } 14 }); 15 16 /* 17 parend 父級id 18 pin 元素id 19 */ 20 function waterfall(parent,pin){ 21 var $aPin = $( "#main>div" ); 22 var iPinW = $aPin.eq( 0 ).width();// 一個塊框pin的寬 23 var num = Math.floor( $( window ).width() / iPinW );//每行中能容納的pin個數【視窗寬度除以一個塊框寬度】 24 //oParent.style.cssText='width:'+iPinW*num+'px;ma rgin:0 auto;';//設置父級居中樣式:定寬+自動水平外邊距 25 $( "#main" ).css({ 26 'width:' : iPinW * num, 27 'margin': '0 auto' 28 }); 29 30 var pinHArr=[];//用於存儲 每列中的所有塊框相加的高度。 31 32 $aPin.each( function( index, value ){ 33 var pinH = $aPin.eq( index ).height(); 34 if( index < num ){ 35 pinHArr[ index ] = pinH; //第一行中的num個塊框pin 先添加進數組pinHArr 36 }else{ 37 var minH = Math.min.apply( null, pinHArr );//數組pinHArr中的最小值minH 38 var minHIndex = $.inArray( minH, pinHArr ); 39 $( value ).css({ 40 'position': 'absolute', 41 'top': minH + 15, 42 'left': $aPin.eq( minHIndex ).position().left 43 }); 44 //數組 最小高元素的高 + 添加上的aPin[i]塊框高 45 pinHArr[ minHIndex ] += $aPin.eq( index ).height() + 15;//更新添加了塊框後的列高 46 } 47 }); 48 } 49 50