js實例教程-Jquery瀑布流插件使用介紹

发布时间:2018-11-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js實例教程-Jquery瀑布流插件使用介紹脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小寶典致力於為廣大程序猿(媛)提供高品質的代碼服務,請大家多多光顧小站,小寶典在此謝過。

瀑布流布局淺析

淺談個人在瀑布流網頁的實現中遇到的問題和解決方法

折騰:瀑布流布局(基於多欄列表流體布局實現)

javascript 瀑布流、各大瀑布流簡析與建議

因為自己用jquery比較多,便萌生了把瀑布流做成插件的想法,圖片就借用迅雷UED上的那些美圖吧。
代碼如下:

;(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+='<p 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"></p>';
}
$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.onload=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.onload=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頁直接查看源文件。
插件使用方法:

. 代碼如下:

$(selector).waterfall(opt); //其中selector為瀑布流容器的選擇器,opt為配置參數對象


所需的html結構:html結構可以就是一個空容器元素,如<p id=”container”></p>,裡面的磚塊元素通過動態加載進來。當然也可以預先放一些磚塊進去,如demo頁中的

. 代碼如下:


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

 

下面詳細說下配置參數對象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等主流瀏覽器。

瀑布流布局淺析

淺談個人在瀑布流網頁的實現中遇到的問題和解決方法

折騰:瀑布流布局(基於多欄列表流體布局實現)

javascript 瀑布流、各大瀑布流簡析與建議

因為自己用jquery比較多,便萌生了把瀑布流做成插件的想法,圖片就借用迅雷UED上的那些美圖吧。
代碼如下:

;(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+='<p 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"></p>';
}
$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.onload=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.onload=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頁直接查看源文件。
插件使用方法:

. 代碼如下:

$(selector).waterfall(opt); //其中selector為瀑布流容器的選擇器,opt為配置參數對象


所需的html結構:html結構可以就是一個空容器元素,如<p id=”container”></p>,裡面的磚塊元素通過動態加載進來。當然也可以預先放一些磚塊進去,如demo頁中的

. 代碼如下:


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

 

下面詳細說下配置參數對象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等主流瀏覽器。

覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!

脚本宝典总结

以上是脚本宝典为你收集整理的js實例教程-Jquery瀑布流插件使用介紹全部内容,希望文章能够帮你解决js實例教程-Jquery瀑布流插件使用介紹所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。