在實際的項目中,偶爾會用到一種佈局——瀑布流佈局。瀑布流佈局的特色是,在多列布局時,能夠保證內容區塊在水平方向上不產生大的空隙,相似瀑布的效果。簡單的說,在垂直列表裏,內容區塊是一個挨着一個的。當內容較多且不固定時,就依賴於html結構的順序,很是受限制。這裏給了一個簡單的例子,只要傳入列表的數量和寬度,就能夠動態的將數據放到對應的列裏。html
原理數組
1.定義兩個容器,一個是存放內容,一個是要展現的列表。app
2.將每列的offsetHeight存入一個數組裏,比較得出最小的那一列,而後把數據放到最小的列裏。判斷當存放內容的容器爲空時,就說明裏面的數據已經所有放到對應的列裏了。函數
注意:這個函數須要在window.onload以後執行,否則每一個內容區塊的高度讀不出來,會致使每一列的offsetHeight不許確。佈局
源代碼:ui
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;"> 5 <title>waterfall佈局</title> 6 <meta name="author" content="rainna" /> 7 <meta name="keywords" content="rainna's js lib" /> 8 <meta name="description" content="waterfall佈局" /> 9 <style> 10 *{margin:0;padding:0;} 11 li{list-style:none;} 12 13 .list li{float:left;min-height:10px;margin:0 0 0 20px;} 14 .list .item{margin:0 0 10px;} 15 .list img{display:block;width:100%;} 16 </style> 17 </head> 18 19 <body> 20 <div class="content" id="content"> 21 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_101.jpg" />01</div> 22 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_102.jpg" />02</div> 23 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_103.jpg" />03</div> 24 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_104.jpg" />04</div> 25 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_105.jpg" />05</div> 26 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_106.jpg" />06</div> 27 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_107.jpg" />07</div> 28 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_108.jpg" />08</div> 29 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_109.jpg" />09</div> 30 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_110.jpg" />10</div> 31 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_111.jpg" />11</div> 32 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_112.jpg" />12</div> 33 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_113.jpg" />13</div> 34 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_114.jpg" />14</div> 35 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_115.jpg" />15</div> 36 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_116.jpg" />16</div> 37 <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_117.jpg" />17</div> 38 </div> 39 <div class="list" id="list"></div> 40 41 <script> 42 var waterFall = { 43 content:document.getElementById('content'), //存放內容的容器 44 list:document.getElementById('list'), //將要展現的列表容器 45 46 setOptions:function(options){ 47 options = options || {}; 48 this.colNum = options.num || 3; //顯示的列數,默認顯示3列 49 this.colWidth = options.width || 200; //每列的寬度 50 }, 51 52 //構建列數 53 setColumn:function(){ 54 var self = this; 55 var html = ''; 56 for(var i=0,l=self.colNum;i<l;i++){ 57 html += '<li style="width:'+ self.colWidth +'px;"></li>'; 58 } 59 self.list.innerHTML = html; 60 61 self.column = self.list.getElementsByTagName('li'); 62 }, 63 64 //計算最小高度的列 65 setMinHeightCol:function(){ 66 var self = this; 67 var heiArray = []; 68 var minIndex = 0,index = 1; 69 for(var i=0,l=self.colNum;i<l;i++){ 70 heiArray[i] = self.column[i].offsetHeight; 71 } 72 while(heiArray[index]){ 73 if(heiArray[index] < heiArray[minIndex]){ 74 minIndex = index; 75 } 76 index ++; 77 } 78 return self.column[minIndex]; 79 }, 80 81 //填充內容 82 setCont:function(cnt){ 83 var self = this; 84 self.setMinHeightCol().appendChild(cnt); 85 if(!!self.content.children[0]){ 86 self.setCont(self.content.children[0]); 87 } 88 }, 89 90 init:function(options){ 91 var self = this; 92 window.onload = function(){ 93 self.setOptions(options); 94 self.setColumn(); 95 self.setCont(self.content.children[0]); 96 } 97 } 98 }; 99 100 waterFall.init(); 101 </script> 102 </body> 103 </html>