直接上代码:

<style>
*{
margin:0;
padding:0;
}
.waterfall_item{
border:solid 3px #ccc;
box-shadow:1px 1px 3px #eee;
border-radius:8px;
font:normal 12px/22px 'Microsoft yahei';
text-align: center;
padding: 20px 0;
overflow: hidden;
}
</style> <script>
/**
瀑布流
分析:
瀑布流主要解决各个列的各元素的位置,以及各列的高度问题
*/
/**
瀑布流
分析:
瀑布流主要解决各个列的各元素的位置,以及各列的高度问题
确定一行有几列,每个元素之间的间隔大小。
获取各元素的高度,添加到面板,并确定每列的高度
属性:
panel//画板
items//要加载的项目
colNum//列数
col=[]//列数组
colLeft// 左间距
colTop// 上间距
方法:
init//初始化
refresh//刷新
setColNum//设置列数
setColLeft//设置左间距
setColTop//设置上间距
paint//添加到外框
refreshPanel//更新外部画板 */
function getStyle(o,name){
if(document.defaultView&&document.defaultView.getComputedStyle)
return document.defaultView.getComputedStyle(o,null)[name];
else
return o.currentStyle[name];
}
function waterfall(opts){
this.init(opts);
}
waterfall.prototype={
constructor:waterfall,
init:function(opts){
this.panel=opts.panel||document.body;//画板
this.items=opts.items||[];//要加载的项目
this.colNum=opts.colNum||3;//列数
this.colLeft=opts.colLeft||5;//左间距
this.colTop=opts.colTop||5;// 上间距
this._updated=false;
this.col=[];
this.initCol();
this.newItems=this.items;
this.draw(this.newItems);
},
initCol:function(){
//初始化列数组
for(var i=0,len=this.colNum;i<len;i++){
this.col[i]={
height:0,//高度
itemNum:0,//项目数
idx:i,
count:0
}
}
},
refresh:function(type){
var items;
if(this._updated){
if(type!=='append'){
this.initCol();
this.panel.innerHTML='';
items=this.items;
}else{
items=this.newItems;
}
this.draw(items);
}
},
_setUpdate:function(updated){
if(this._updated!=updated){
this._updated=updated;
}
},
setColNum:function(num){
if(num==this.colNum)return;
this._setUpdate(true);
this.colNum=num;
},
setColLeft:function(leftSpan){
this._setUpdate(true);
this.colLeft=leftSpan;
},
setColTop:function(topSpan){
this._setUpdate(true);
this.colLeft=leftSpan;
},
addItems:function(items){
this._setUpdate(true);
this.items=this.items.concat(items);
this.newItems=items;
},
draw:function(items){
var that=this,
i=0,
p=this.panel,
len=items.length;
for(;i<len;i++){
var r=Math.floor(i/this.colNum);
var w=parseInt(getStyle(p,'width'))/this.colNum-this.colLeft;
var curCol=this.getMinHeightCol();
var c=curCol.idx;
var t=curCol.height+this.colTop;
var l=c*(w+this.colLeft)+this.colLeft;
var oitem=this.createItem(w,l,t,items[i]);
p.appendChild(oitem);
var itemH=oitem.offsetHeight;
curCol.height=curCol.height+itemH+this.colTop;
curCol.count++;
p.style.height=curCol.height+'px';
}
},
createItem:function(w,l,t,html){
var oitem=document.createElement('div');
oitem.innerHTML=html;
oitem=oitem.firstChild;
oitem.style.position='absolute';
oitem.style.width=w+'px';
oitem.style.left=l+'px';
oitem.style.top=t+'px';
return oitem;
},
getMinHeightCol:function(){
var idx=0,minner=this.col[idx].height;
for(var i=0,len=this.colNum;i<len;i++){
if(minner>this.col[i].height){
minner=this.col[i].height;
idx=i; }
}
return this.col[idx];
}
} </script>
<div style="margin:0 auto">列:<input type="text" value='3' id="col_num" /><button id="refresh">刷新</button></div>
<div id="response_flow" class="response_flow" style="width:1000px;position:relative;"> </div>
<script>
//测试用例
var items=[
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' style='height:100px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='1.jpg' height='200' style='height:200px;' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>"
]
var colNum;
var refreshBtn=document.getElementById('refresh'); var panel=document.getElementById('response_flow'); var flow=new waterfall({
panel:panel,
items:items,
colLeft:20,
topLeft:20
})
//追加
var newItems=[
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='100' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'><img src='http://sandbox.runjs.cn/uploads/rs/392/w5zrkmek/1.jpg' height='200' /><br />asdfasdfasdf</div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>",
"<div class='waterfall_item'>asdfasdfasdf<br />asdfasdfasdf<br /></div>"
]; refreshBtn.onclick=function(){
colNum=document.getElementById('col_num').value;
flow.setColNum(colNum);
flow.refresh();
}
var toop=200;
var loadNum=1;
var maxNum=5;
var originTop=document.body.scrollHeight-document.documentElement.clientHeight;
window.onscroll=function(){
var to=document.body.scrollTop;
if(loadNum>maxNum)return;
if(to>loadNum*toop){
loadNum++;
flow.addItems(newItems)
flow.refresh('append')
}
}
window.onresize=function(){
flow._setUpdate(true);
flow.refresh();
} </script>

效果见:

javascript-实现简单瀑布流的更多相关文章

  1. iOS之简单瀑布流的实现

    iOS之简单瀑布流的实现   前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里. 实现思路 collectionView能实现各中吊炸天的布局,其精髓就在于UICollectionVie ...

  2. jquery实现简单瀑布流

    瀑布流这个概念一直不是很理解,看到别人可以实现,自己弄了很久还是不能实现就很纠结.瀑布流这根刺就一直扎在我心里,一次偶然的机会看到别人实现了瀑布流,我想我是不是也应该再继续把这个未完成的任务画一个圆满 ...

  3. WPF下制作的简单瀑布流效果

    最近又在搞点小东西,美化界面的时候发现瀑布流效果比较不错.顺便就搬到了WPF,下面是界面 我对WEB前端不熟,JS和CSS怎么实现的,我没去研究过,这里就说下WPF的实现思路,相当简单. 1.最重要的 ...

  4. JavaScript:实现瀑布流

    一.前言: 瀑布流现在是一个非常常用的布局方式了,尤其在购物平台上,例如蘑菇街,淘宝等等. 二.流程: 1.在html文件中写出布局的元素内容: 2.在css文件中整体对每一个必要的元素进行样式和浮动 ...

  5. jquery实现简单瀑布流代码

    测试环境:ie8 ff13.0.1  chrome22 可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度 <!DOCTYP ...

  6. Jquery简单瀑布流代码示例

    最近很多网站都采用瀑布流风格设计,感觉挺有个性的,比较合适做图片类型的网站,没事仿开心网做一个瀑布流示例. 需要用到Jquery,jquery.masonry.min.js <!DOCTYPE ...

  7. 用原生JavaScript实现图片瀑布流的浏览效果

    学习JS,活跃思维,灵活运用的一个较为典型的学习案例.同一个瀑布流的效果但实现方式却很多,利用递归.冒泡等等手法都可以达到你想要的目的.这次要说的就是利用类似递归来实现此效果的原创方案.此方案个人认为 ...

  8. 从零开始学习前端JAVASCRIPT — 11、JavaScript运动模型及轮播图效果、放大镜效果、自适应瀑布流

    未完待续...... 一.运动原理 通过连续不断的改变物体的位置,而发生移动变化. 使用setInterval实现. 匀速运动:速度值一直保持不变. 多物体同时运动:将定时器绑设置为对象的一个属性. ...

  9. thinphp下拉获取更多瀑布流效果

    html页面 <body> <script type="text/javascript" src="jquery.min.js">< ...

随机推荐

  1. c/c++中内存对齐完全理解

    一,什么是内存对齐?内存对齐用来做什么? 所谓内存对齐,是为了让内存存取更有效率而采用的一种编译阶段优化内存存取的手段. 比如对于int x;(这里假设sizeof(int)==4),因为cpu对内存 ...

  2. django数据库设计

    1 知识点 主要是分析设计数据库的数据表和数据表字段,然后使用Navicat Data Modeler创建模 将sqlite数据库修改成mysql数据库,同步数据 2 模型 2.1 数据表所有的数据列 ...

  3. springboot如何直接读取webapp下页面?

    公司改用springboot的时候,将页面相关的文件都放在了src/main/webapp下,我直接通过main方式启动的时候,无法读取到src/mian/webapp下文件,但是通过spring-b ...

  4. ionicframework I ------------- 初体验

    ionicframework I -------------  初体验 Create hybrid mobile apps with the web technologies you love. Fr ...

  5. oracle的order by decode根据文字自定义排序的例子

    oracle的order by decode根据文字自定义排序的例子: order by decode(t.title, '当前生效预警', 1, '今日即将生效', 2, '明日预计生效', 3, ...

  6. cookie的简单留言框

    我们在网页浏览时退出后,再次进入时会有上次的记录,这就用的上cookie属性了,cookie就是服务器暂存放在你计算机上的一笔资料,好让服务器用来辨认你的计算机.当你在浏览网站的时候,Web服务器会先 ...

  7. 删除Windows服务

    或者开始→运行 ,输入“regedit”,在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services 下找到需要删除的服务名,直接删除即可.

  8. mysql乐观锁总结和实践(转)

    原文:mysql乐观锁总结和实践 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任何场景,它也有它存在的一些不足,因为悲观锁大多数情况下依靠数据库的 ...

  9. Leetcode 之 Keys Keyboard

    1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...

  10. centos Docker安装前升级内核3.10的方法

    首先我虚拟机系统都是Centos 6.5 .ESXI ,后安装devel .ESXI 后来 .ESXI 我所操作的都是虚拟机,但是在真实机上面如何我就不清楚了~~ 大家一定要记得安装步骤,,,不然就是 ...