标签:des style blog http color os io strong for
这个例子与上一篇类似,唯一的区别是排序的方式有差别。上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容。
两种方法各有优缺点。第一种需要在图片内容加载完成的情况下有效,各个列的图高度差异不大。这个例子不需要在window.onload之后执行,直接计算每列的索引,按照顺序一列一列的放置,在图大小不同的时候列的高度可能会有很大的差距。用下面的图做说明。
说明:
上一个案例如下图:
源代码:
<!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;} p{margin:20px;} .list li{float:left;min-height:10px;margin:0 0 0 20px;} .list .item{margin:0 0 10px;} .list img{display:block;width:100%;} #content{display:none;} </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‘); }, //填充内容 setCont:function(cnt){ var self = this; self.index = self.index%self.colNum || 0; //将要放置内容的列的索引,默认从0开始 self.column[self.index].appendChild(cnt); self.index ++; if(!!self.content.children[0]){ self.setCont(self.content.children[0]); } }, init:function(options){ var self = this; self.setOptions(options); self.setColumn(); self.setCont(self.content.children[0]); } }; waterFall.init(); </script> </body> </html>
出处:http://www.cnblogs.com/zourong/p/3934738.html
标签:des style blog http color os io strong for
原文地址:http://www.cnblogs.com/mq0036/p/3934850.html