在實際的項目中,偶爾會用到一種佈局——瀑布流佈局。瀑布流佈局的特色是,在多列布局時,能夠保證內容區塊在水平方向上不產生大的空隙,相似瀑布的效果。簡單的說,在垂直列表裏,內容區塊是一個挨着一個的。當內容較多且不固定時,就依賴於html結構的順序,很是受限制。這裏給了一個簡單的例子,只要傳入列表的數量和寬度,就能夠動態的將數據放到對應的列裏。html
原理數組
1.定義兩個容器,一個是存放內容,一個是要展現的列表。app
2.將每列的offsetHeight存入一個數組裏,比較得出最小的那一列,而後把數據放到最小的列裏。判斷當存放內容的容器爲空時,就說明裏面的數據已經所有放到對應的列裏了。函數
注意:這個函數須要在window.onload以後執行,否則每一個內容區塊的高度讀不出來,會致使每一列的offsetHeight不許確。佈局
源代碼:ui
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;"> <title>waterfall佈局</title> <meta name="author" content="rainna" /> <meta name="keywords" content="rainna's js lib" /> <meta name="description" content="waterfall佈局" /> <style> *{margin:0;padding:0;} li{list-style:none;} .list li{float:left;min-height:10px;margin:0 0 0 20px;} .list .item{margin:0 0 10px;} .list img{display:block;width:100%;} </style> </head> <body> <div class="content" id="content"> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_101.jpg" />01</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_102.jpg" />02</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_103.jpg" />03</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_104.jpg" />04</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_105.jpg" />05</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_106.jpg" />06</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_107.jpg" />07</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_108.jpg" />08</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_109.jpg" />09</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_110.jpg" />10</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_111.jpg" />11</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_112.jpg" />12</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_113.jpg" />13</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_114.jpg" />14</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_115.jpg" />15</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_116.jpg" />16</div> <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_117.jpg" />17</div> </div> <div class="list" id="list"></div> <script> var waterFall = { content:document.getElementById('content'), //存放內容的容器 list:document.getElementById('list'), //將要展現的列表容器 setOptions:function(options){ options = options || {}; this.colNum = options.num || 3; //顯示的列數,默認顯示3列 this.colWidth = options.width || 200; //每列的寬度 }, //構建列數 setColumn:function(){ var self = this; var html = ''; for(var i=0,l=self.colNum;i<l;i++){ html += '<li style="width:'+ self.colWidth +'px;"></li>'; } self.list.innerHTML = html; self.column = self.list.getElementsByTagName('li'); }, //計算最小高度的列 setMinHeightCol:function(){ var self = this; var heiArray = []; var minIndex = 0,index = 1; for(var i=0,l=self.colNum;i<l;i++){ heiArray[i] = self.column[i].offsetHeight; } while(heiArray[index]){ if(heiArray[index] < heiArray[minIndex]){ minIndex = index; } index ++; } return self.column[minIndex]; }, //填充內容 setCont:function(cnt){ var self = this; self.setMinHeightCol().appendChild(cnt); if(!!self.content.children[0]){ self.setCont(self.content.children[0]); } }, init:function(options){ var self = this; window.onload = function(){ self.setOptions(options); self.setColumn(); self.setCont(self.content.children[0]); } } }; waterFall.init();//使用初始化參數 waterFall.init({num:4,width:100}); </script> </body> </html>
出處:http://www.cnblogs.com/zourong/p/3934661.htmlthis