码迷,mamicode.com
首页 > Web开发 > 详细

JS案例之6——瀑布流布局(1)

时间:2014-08-22 19:24:39      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   for   ar   

 

 

  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;width:200px;min-height:10px;margin:0 0 0 20px;}
 14 .list .item{margin:0 0 10px;}
 15 .list img{display:block;}
 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_002.jpg" />02</div>
 23     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_003.jpg" />03</div>
 24     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_004.jpg" />04</div>
 25     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_005.jpg" />05</div>
 26     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_006.jpg" />06</div>
 27     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_007.jpg" />07</div>
 28     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_008.jpg" />08</div>
 29     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_009.jpg" />09</div>
 30     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_010.jpg" />10</div>
 31     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_011.jpg" />11</div>
 32     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_012.jpg" />12</div>
 33     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_013.jpg" />13</div>
 34     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_014.jpg" />14</div>
 35     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_015.jpg" />15</div>
 36     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_016.jpg" />16</div>
 37     <div class="item"><img src="http://cued.xunlei.com/demos/publ/img/P_016.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     },
 50     
 51     //构建列数
 52     setColumn:function(){
 53         var self = this;
 54         var html = ‘‘;
 55         for(var i=0,l=self.colNum;i<l;i++){
 56             html += <li></li>;
 57         }
 58         self.list.innerHTML = html;
 59         
 60         self.column = self.list.getElementsByTagName(li);
 61     },
 62     
 63     //计算最小高度的列
 64     setMinHeightCol:function(){
 65         var self = this;
 66         var heiArray = [];
 67         var minIndex = 0,index  = 1;
 68         for(var i=0,l=self.colNum;i<l;i++){
 69             heiArray[i] = self.column[i].offsetHeight;
 70         }
 71         while(heiArray[index]){
 72             if(heiArray[index] < heiArray[minIndex]){
 73                 minIndex = index;
 74             }
 75             index ++;
 76         }
 77         return self.column[minIndex];
 78     },
 79     
 80     //填充内容
 81     setCont:function(cnt){
 82         var self = this;
 83         self.setMinHeightCol().appendChild(cnt);
 84         if(!!self.content.children[0]){
 85             self.setCont(self.content.children[0]);
 86         }
 87     },
 88     
 89     init:function(options){
 90         var self = this;
 91         window.onload = function(){
 92             self.setOptions(options);
 93             self.setColumn();
 94             self.setCont(self.content.children[0]);
 95         }
 96     }
 97 };
 98 
 99 waterFall.init();
100 </script>
101 </body>
102 </html>

 

JS案例之6——瀑布流布局(1)

标签:des   style   blog   http   color   os   io   for   ar   

原文地址:http://www.cnblogs.com/zourong/p/3929999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!