在实际的项目中,偶尔会用到一种布局——瀑布流布局。瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果。简单的说,在垂直列表里,内容区块是一个挨着一个的。当内容较多且不固定时,就依赖于html结构的顺序,非常受限制。这里给了一个简单的例子,只要传入列表的数量和宽度,就可以动态的将数据放到对应的列里。

原理

1.定义两个容器,一个是存放内容,一个是要展示的列表。

2.将每列的offsetHeight存入一个数组里,比较得出最小的那一列,然后把数据放到最小的列里。判断当存放内容的容器为空时,就说明里面的数据已经全部放到对应的列里了。

注意:这个函数需要在window.onload之后执行,不然每个内容区块的高度读不出来,会导致每一列的offsetHeight不准确。

源代码:

<!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.html

JS瀑布流布局模式(1)的更多相关文章

  1. JS瀑布流布局模式(2)

    这个例子与上一篇类似,唯一的区别是排序的方式有差别.上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容. 两种方法各有优缺点.第一种需要在图片内容加载完成的情况下有效,各个列的图高度差异不大. ...

  2. JS 瀑布流布局

    瀑布流布局 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  3. JS瀑布流布局

    好久没有更新博客喽,今天来说一个瀑布流布局. 瀑布流在很多网站已有很多,现在只说一下简单的实现原理吧, 1.计算一行可以排放几个元素 2.建立一个数组用于存放第一行的每个元素的高度. 3.得到数组中的 ...

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

    在实际的项目中,偶尔会用到一种布局——瀑布流布局.瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果.简单的说,在垂直列表里,内容区块是一个挨着一个的.当内容 ...

  5. 纯js实现瀑布流布局及ajax动态新增数据

    本文用纯js代码手写一个瀑布流网页效果,初步实现一个基本的瀑布流布局,以及滚动到底部后模拟ajax数据加载新图片功能. 缺点: 1. 程序不是响应式,不能实时调整页面宽度: 2. 程序中当新增ajax ...

  6. js网页瀑布流布局

    瀑布流布局思路: 1.css样式,图片的父级div样式设置为定位或者浮动 2.找出图片父级元素(box)和最外元素(main):获取box的宽度和main的宽,然后计算main容器一行能容纳多少个bo ...

  7. 瀑布流布局js

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  8. 也来谈谈wap端瀑布流布局

    Definition 瀑布流布局,在视觉上表现为参差不齐的多栏布局,随着页面滚动条向下滚动,新数据不断被加载进来. 瀑布流对于图片的展现,是高效而具有吸引力的,用户一眼扫过的快速阅读模式可以在短时间内 ...

  9. Bootstrap实战 - 瀑布流布局

    讲 Bootstrap 基础的教程网上已经很多了,实际上 Bootstrap 中文网(bootcss.com)里的文档已经写的很详细了,但实战的案例却不多.这里用一些当前流行的网页布局为导向,使用 B ...

随机推荐

  1. Linux下ntpdate时间同步

    Linux下ntpdate时间同步 Ntp服务器配置(暂略,以后整理) 时间同步方法 同步命令               # ntpdate ntp服务器域名或IP           例:# nt ...

  2. PHP遍历数组

    foreach PHP代码: <?php   $url = array( '新浪' =>'www.sina.com' ,                    '雅虎' =>'www ...

  3. hdu 1850 Being a Good Boy in Spring Festival(Nimm Game)

    题意:Nimm Game 思路:Nimm Game #include<iostream> #include<stdio.h> using namespace std; int ...

  4. 关于COUNT STOPKEY的工作机制(转载)

    SQL> select rownum rn ,a.* from cnmir.ew_auctions a where rownum<50000; Execution Plan-------- ...

  5. java线程实践记录

    框架构建过程中遇到需要用到线程的地方,虽然以前经常听到线程,也看过一些线程类的文章,但真正使用时还是遇到一些问题,此篇正式为了记录自己对线程实操的体会. 入口类代码: public class tes ...

  6. selenium + python 多浏览器测试

    selenium + python 多浏览器测试 支持库包 在学习 Python + Selenium 正篇之前,先来看下对多浏览器模拟的支持.目前selenium包中已包含webdriver,hel ...

  7. Ubuntu上用快捷键关闭没有响应的程序

    Linux 上有很多方法可以强制关闭无响应的程序,比如你可以通过按快捷键 Ctrl + Shift + T 来调出 Terminal 或者用 Ctrl + Shift + F1 进入 Console ...

  8. erlang常用命令

    1 erlang启动时就运行odbc erl -s odbc 2 ping 节点 net_adm:ping('rabbit@COMPUTERNAME'). 3 运行cmd命令 os:cmd(" ...

  9. Maven 包命令

    1.必须选中项目,然后单击Run As,选择Maven build. 2.在配置窗体中的Goals栏填写clean package. 注意:Installed JREs中配置的JREs的位置必须是JD ...

  10. jquery让一个点击事件刷新页面就自己执行一次的方法

    $('name')这个元素之前已经绑定过事件啦,(on绑定)然后直接调用下即可: $('name').click();