瀑布流布局在目前貌似很火爆,具体的分析、原理、用到的知识等等可以看看以下几位牛人写的东西。

瀑布流布局浅析

浅谈个人在瀑布流网页的实现中遇到的问题和解决方法

折腾:瀑布流布局(基于多栏列表流体布局实现)

javascript 瀑布流、各大瀑布流简析与建议

因为自己用jquery比较多,便萌生了把瀑布流做成插件的想法,图片就借用迅雷UED上的那些美图吧。

先看看Demo

把代码放出来吧

(function($){var//参数setting={column_width:204,//列宽column_className:'waterfall_column',//列的类名column_space:10,//列间距cell_selector:'.cell',//要排列的砖块的选择器,限定在瀑布流的容器内img_selector:'img',//要加载的图片的选择器auto_imgHeight:true,//是否需要自动计算图片的高度fadein:true,//是否渐显载入fadein_speed:600,//渐显速率,单位毫秒insert_type:1, //砖块插入方式,1为插入最短那列,2为按序轮流插入getResource:function(index){ }  //获取动态资源函数,必须返回一个砖块元素集合,传入参数为加载的次数},//waterfall=$.waterfall={},$container=null;//容器waterfall.load_index=0, //加载次数$.fn.extend({waterfall:function(opt){opt=opt||{};  setting=$.extend(setting,opt);$container=waterfall.$container=$(this);waterfall.$columns=creatColumn();render($(this).find(setting.cell_selector).detach(),false); //重排已存在元素时强制不渐显waterfall._scrollTimer2=null;$(window).bind('scroll',function(){clearTimeout(waterfall._scrollTimer2);waterfall._scrollTimer2=setTimeout(onScroll,300);});waterfall._scrollTimer3=null;$(window).bind('resize',function(){clearTimeout(waterfall._scrollTimer3);waterfall._scrollTimer3=setTimeout(onResize,300);});}});function creatColumn(){//创建列waterfall.column_num=calculateColumns();//列数//循环创建列var html='';for(var i=0;i<waterfall.column_num;i++){html+='<div class="'+setting.column_className+'" style="width:'+setting.column_width+'px; display:inline-block; *display:inline;zoom:1; margin-left:'+setting.column_space/2+'px;margin-right:'+setting.column_space/2+'px; vertical-align:top; overflow:hidden"></div>';}$container.prepend(html);//插入列return $('.'+setting.column_className,$container);//列集合}function calculateColumns(){//计算需要的列数var num=Math.floor(($container.innerWidth())/(setting.column_width+setting.column_space));if(num<1){ num=1; } //保证至少有一列return num;}function render(elements,fadein){//渲染元素if(!$(elements).length) return;//没有元素var $columns = waterfall.$columns;$(elements).each(function(i){                                     if(!setting.auto_imgHeight||setting.insert_type==2){//如果给出了图片高度,或者是按顺序插入,则不必等图片加载完就能计算列的高度了if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素}else if(setting.insert_type==2){insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素   }return true;//continue}                     if($(this)[0].nodeName.toLowerCase()=='img'||$(this).find(setting.img_selector).length>0){//本身是图片或含有图片var image=new Image;var src=$(this)[0].nodeName.toLowerCase()=='img'?$(this).attr('src'):$(this).find(setting.img_selector).attr('src');image.οnlοad=function(){//图片加载后才能自动计算出尺寸image.onreadystatechange=null;if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素}else if(setting.insert_type==2){insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素  }image=null;}image.onreadystatechange=function(){//处理IE等浏览器的缓存问题:图片缓存后不会再触发onload事件if(image.readyState == "complete"){image.οnlοad=null;if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素}else if(setting.insert_type==2){insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素   }image=null;}}image.src=src;}else{//不用考虑图片加载if(setting.insert_type==1){ insert($(elements).eq(i),setting.fadein&&fadein);//插入元素}else if(setting.insert_type==2){insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素  }}                     });}function public_render(elem){//异步数据渲染接口函数      render(elem,true);}function insert($element,fadein){//把元素插入最短列if(fadein){//渐显$element.css('opacity',0).appendTo(waterfall.$columns.eq(calculateLowest())).fadeTo(setting.fadein_speed,1);}else{//不渐显$element.appendTo(waterfall.$columns.eq(calculateLowest()));}}function insert2($element,i,fadein){//按序轮流插入元素if(fadein){//渐显$element.css('opacity',0).appendTo(waterfall.$columns.eq(i%waterfall.column_num)).fadeTo(setting.fadein_speed,1);}else{//不渐显$element.appendTo(waterfall.$columns.eq(i%waterfall.column_num));}}function calculateLowest(){//计算最短的那列的索引var min=waterfall.$columns.eq(0).outerHeight(),min_key=0;waterfall.$columns.each(function(i){                         if($(this).outerHeight()<min){min=$(this).outerHeight();min_key=i;}                             });return min_key;}function getElements(){//获取资源$.waterfall.load_index++;return setting.getResource($.waterfall.load_index,public_render);}waterfall._scrollTimer=null;//延迟滚动加载计时器function onScroll(){//滚动加载clearTimeout(waterfall._scrollTimer);waterfall._scrollTimer=setTimeout(function(){var $lowest_column=waterfall.$columns.eq(calculateLowest());//最短列var bottom=$lowest_column.offset().top+$lowest_column.outerHeight();//最短列底部距离浏览器窗口顶部的距离var scrollTop=document.documentElement.scrollTop||document.body.scrollTop||0;//滚动条距离var windowHeight=document.documentElement.clientHeight||document.body.clientHeight||0;//窗口高度if(scrollTop>=bottom-windowHeight){render(getElements(),true);}},100);}function onResize(){//窗口缩放时重新排列if(calculateColumns()==waterfall.column_num) return; //列数未改变,不需要重排var $cells=waterfall.$container.find(setting.cell_selector);waterfall.$columns.remove();waterfall.$columns=creatColumn();render($cells,false); //重排已有元素时强制不渐显}
})(jQuery);

貌似把代码贴进来格式有点乱了,哎先不管了。上面的代码要是看不清可以在demo页直接查看源文件。

插件使用方法:

?

1

$(selector).waterfall(opt); //其中selector为瀑布流容器的选择器,opt为配置参数对象

所需的html结构:html结构可以就是一个空容器元素,如<div id=”container”></div>,里面的砖块元素通过动态加载进来。当然也可以预先放一些砖块进去,如demo页中的

<div id="container"><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_000.jpg" /><p>00</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_001.jpg" /><p>01</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_002.jpg" /><p>02</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_003.jpg" /><p>03</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_004.jpg" /><p>04</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_005.jpg" /><p>05</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_006.jpg" /><p>06</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_007.jpg" /><p>07</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_008.jpg" /><p>08</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_009.jpg" /><p>09</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_010.jpg" /><p>10</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_011.jpg" /><p>11</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_012.jpg" /><p>12</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_013.jpg" /><p>13</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_014.jpg" /><p>14</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_015.jpg" /><p>15</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_016.jpg" /><p>16</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_017.jpg" /><p>17</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_018.jpg" /><p>18</p></div><div class="cell"><img src="http://cued.xunlei.com/demos/publ/img/P_019.jpg" /><p>19</p></div>
</div>

下面详细说下配置参数对象opt的各属性的作用及其默认值。

column_width:204  //瀑布流是由列组成的,该参数规定了每列的宽度,该参数会直接影响到瀑布流的列数

column_className:'waterfall_column'   //列的类名,便于自定义样式

column_space:10    //列与列之间的间距

cell_selector:'.cell'   //要排列的砖块的选择器,限定在瀑布流的容器内,即插件是通过这个选择器来获取砖块元素的,并且是在瀑布流的容器内来查找这个选择器匹配的元素。

img_selector:'img'  //要加载的图片的选择器。如果你的瀑布流要加载的砖块元素的主题内容是大小不固定的图片,则该参数就是这些图片的选择器,插件需要获取这些图片来进行计算。

auto_imgHeight:true  //是否需要自动计算图片的高度,如果图片的大小是固定的,则把该参数设为false吧

fadein:true  //是否渐显载入

fadein_speed:600  //渐显速率,单位毫秒

insert_type:1  //砖块插入方式,1为插入最短那列,2为按序轮流插入

getResource:function(index,render){ } //获取动态资源函数,必须返回一个砖块元素集合,传入的第一个参数index为已加载的次数,第二个参数为渲染函数,它可以接受一个砖头元素集合作为参数,如果是使用ajax加载数据,则得到数据后要手动调用该函数来进行渲染 。每次到达瀑布流底部时会自动触发该函数来加载更多资源。

吐槽时间:

瀑布流加载的内容一般都宽度相同,高度不同的图片,如果能预先知道图片的高度,那就简单多了,但如果不能,则必须等到图片加载后才能计算出图片的高度,这是瀑布流最烦人的地方,也正是因为这样,如果是那些不知道高度的图片,则插入的顺序可能会有些混乱,而且每次刷新顺序都不同,因为每张图片加载完成的先后顺序并不是固定的,也许这次这个快一点,下次那个快一点。所以如果图片高度事先不知道,则整个砖块的高度也会不知道,必须等砖块里的图片加载完成后才能算出砖块的高度。如果是这样但又想保证砖块的插入顺序,则建议使用按顺序轮流插入的方式插入砖块,即把insert_type参数设为2。因为是插件,所以要考虑使用简便,但使用起来越简便,插件内部就会越复杂,漏洞、bug也会增多,所以我会继续完善这个插件。

本插件支持IE6+、chrome、firefox、opera、safari等主流浏览器。

原文地址:http://www.cnblogs.com/2050/archive/2012/05/03/2480702.html

jQuery 瀑布流插件相关推荐

  1. Jquery瀑布流插件wookmark

    瀑布流布局在现在的网页设计里面可谓是相当的流行的,从网页到手机应用,都能见到瀑布流的身影.相比较手机应用来说,感觉网页上的瀑布流布局效果实现简单很多,因为我们有现成的jQuery插件可以用,只要稍微的 ...

  2. 写了个jquery瀑布流插件 , 求各位道友猛男指点指点

    如题 ,请各位大佬指点一下 ;;(function(){if(typeof jQuery === "function"){(function( factory , $ ){if($ ...

  3. jQuery 瀑布流插件: Wookmark

    原文链接: jQuery Wookmark 在线示例: jQuery Wookmark Demo Wookmark官网:  http://www.wookmark.com/jquery-plugin ...

  4. html流式布局插件,Jquery瀑布流网格布局插件

    插件描述:一款简单且高度可定制的jQuery瀑布流网格布局插件.通过该瀑布流网格插件你可以动态添加和删除各种尺寸的图片,定义图片宽度,设置网格的列数,或使用流式布局方式,甚至还可以通过URL动态添加图 ...

  5. 瀑布流插件|jquery.masonry|使用demo

    Maonsry+Infinite-Scroll实现滚动式分页,网上有很多,这里只说: 瀑布流插件的一个基本使用,附上基本功能的demo <html> <head> <me ...

  6. masonry ajax瀑布流,jquery.masonry瀑布流插件的4个使用步骤

    解释:很简单,就是把下载之后的脚本文件嵌入到你想使用2.瀑布流布局样式代码 .container-fluid { padding: 20px; } .box { margin-bottom: 20px ...

  7. waterfall-Jquery瀑布流插件,Jquery瀑流式插件

    瀑布流插件布局在目前貌似很火爆,因为自己用jquery插件比较多,便萌生了把瀑布流插件实现的想法,图片就借用迅雷UED上的那些美图吧. 预览地址:http://www.sitejs.cn/code/d ...

  8. html5 原生插件,前端必备插件之纯原生JS的瀑布流插件Macy.js

    这是一款非常轻量级的纯原生JS的瀑布流插件--Macy.js,如今图片和视频网站非常多,非常适应瀑布流这样的布局方式来呈现给用户. 这款流布局JS插件仅有4KB的大小,可以说是非常轻量级的哦.配置也比 ...

  9. jquery 瀑布流实例最流行瀑布流图片展示

    jquery masonry与infinitescroll两款瀑布流插件制作当下最流行的瀑布流图片展示实例,通过鼠标滚动图片无限加载的类似瀑布的效果的图片展示.用户可以无限浏览图片或内容无限加载瀑布流 ...

  10. jaliswall.js图片瀑布流插件

    下载地址 jaliswall.js图片瀑布流插件,jQuery响应式瀑布流布插件jaliswall dd:

最新文章

  1. [转载红鱼儿]kbmmw 开发点滴:ErrorTable用法
  2. 求职Python开发,面试官最喜欢问的几个问题
  3. NoSQL(3) 之Redis主从复制、哨兵和集群介绍及详细搭建步骤
  4. 记录 之 Argparse 中的 可选参数 action 用法
  5. linux samba服务器
  6. apache fop_Apache FOP与Eclipse和OSGi的集成
  7. 第三十七期:如果你这样回答“什么是线程安全”,面试官都会对你刮目相看
  8. away3d 4.0.9Gold 学习笔记 加载方法总结(4)
  9. eclipse html tab宽度,Eclipse 设置Tab键为4个空格
  10. 帆软报表实现Excel导入,并校验数据
  11. 模板题——KMP Trie树 并查集
  12. WinUsbNet: A managed interface to WinUSB.sys
  13. xposed框架_免ROOT 用“太极”替代Xposed框架
  14. 解决win7旗舰版无法卸载IE11,恢复系统原内置IE版本浏览器(本人原内置IE8)
  15. 会议论文有没有影响因子_关于论文是否被SCI、Ei、ISTP等检索以及期刊影响因子的解说...
  16. python画画excel_用Python在Excel里画出蒙娜丽莎的方法示例
  17. iOS应用出现未验证应用的问题排查
  18. 使用FFmpeg 编解码 FLV的HEVC(H265)格式的视频
  19. 从一个远程服务器的mysql数据库表的数据复制到本地电脑mysql数据库表,两张表结构一样就是复制数据...
  20. python处理金融数据_Python金融大数据分析-数据获取与简单处理

热门文章

  1. 高并发之阿里云弹性伸缩的使用记录
  2. 道德经和译文_道德经——老子道德经全文及译文
  3. lav点搜网metro风格分享
  4. Vue 扫描二维码、条形码
  5. c语言ascii码字符串转16进制,C语言 16进制与ascii码互转
  6. Android桌面插件的开发
  7. R语言学习-问题解决-Error in output$nodeID : $ operator is invalid for atomic vectors
  8. css 一直图片适配所有手机背景图
  9. Linux系统关闭virbr0
  10. AiHi+翼次元学院儿童自然感知实践“几米小甜田”+中韧开智“优慧码”| Wit++