jQuery實(shí)現(xiàn)瀑布流的取巧做法分享
分析:瀑布流,做法有2種
(1)絕對(duì)定位方案:每個(gè)單元格設(shè)置為絕對(duì)定位,通過計(jì)算,分別設(shè)置 top , left 即可實(shí)現(xiàn)
(2)浮動(dòng)方案:弄N列布局(浮動(dòng)),然后圖片數(shù)據(jù),按順序依次插入,如N為3列 ,第一張圖片插入到第一列,第二張圖片插入到第二列,第三張圖片插入到第三列,第四張圖片插入到第一列........這樣循環(huán)插入(不能自適應(yīng))
CSS與HTML代碼:
body,ul,li{margin:0;margin:0;}
ul{list-style:none;}
.clearfix:after{visibility:hidden;display:block;font-size:0;content:" ";clear:both; height:0;}.clearfix{*zoom:1}
/*瀑布流*/
.wallList{width:860px;}
.wallList li{float:left;display:inline;margin-right:16px;}
.wallList li a{background:#eee;border:1px solid #ccc;padding:5px 5px 0 5px;display:block;margin-bottom:10px;}
.wallList li a:hover{border-color:#f60;}
.wallList li .name{display:block;text-align:center;padding:8px 0;}
.loadTips{text-align:center;padding:15px 0;}
<div class="wallList" id="wallList">
<ul class="clearfix">
<li></li>
<li></li>
<li></li>
</ul>
<p class="loadTips" id="loadTips"><span>正在加載......</span></p>
</div>
使用jQuery實(shí)現(xiàn),大概思路如下:
?。?)獲取N列中 最小的高度值,JS提供的API是Math.min(),但這個(gè)API最多只能傳入 2 個(gè)參數(shù),所以就需要用aplly來擴(kuò)展,Math.min.apply(null,[xxx,xxx,xxxx,xxxx])
?。?)給 window 綁定 scroll事件,下拉的時(shí)候獲取 $(document).scrollTop() , 當(dāng) $(document).scrollTop() 大于 最小的高度值,就ajax請(qǐng)求url,如果有數(shù)據(jù),就往頁面插入HTML結(jié)構(gòu),沒有則提示 “加載完”,然后window解綁此事件
// 數(shù)據(jù)格式
var testJson = {
"status":1,
"data":[
{"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" {"href":"http:xxxxxxx","src":" ]
}
var wallPic = function(){
var $target = $('#wallList'),
$li = $target.find('li'),
$tips = $('#loadTips'),
oTop = 0,//滾動(dòng)判斷的值
row = 3,//列數(shù)
page = 1,//ajax請(qǐng)求的頁碼值
url = 'xxxx', //ajax請(qǐng)求地址
on_off = true; //插入結(jié)構(gòu)的開關(guān),防止ajax錯(cuò)誤性多次加載數(shù)據(jù)
return{
fillData:function(callback){
var _that = this;
on_off = false;
/* ajax
--------------------*/
// $.get(url,{ page:page,count:30 },function(json){
// if(json.status==1){
// _that.appendHTML(json.data);
// on_off = true;
// page++;
// }else{
// _that.loadedTips();
// }
// },'json');
/* 模擬測(cè)試-設(shè)置定時(shí)器模擬ajax請(qǐng)求數(shù)據(jù)
-----------------------*/
setTimeout(function(){
// 模擬終止
if(page==3){
_that.loadedTips();
return;
}
_that.appendHTML(testJson.data);
on_off = true;
page++;
},400);
},
appendHTML:function(data){
var len = data.length,
n = 0;
for(;n<len;n++){
var k = 0;
n>(row-1)?k=n%row:k=n;
$li[k].innerHTML += '<a href="'+data[n].href+'" target="_blank"><img src="'+data[n].src+'" width="'+data[n].width+'" height="'+data[n].height+'" alt="'+data[n].name+'" /><span class="name">'+data[n].name+'</span></a>';
}
this.getOTop();
},
getOTop:function(){
oTop = Math.min.apply(null,[$li.eq(0).height(),$li.eq(1).height(),$li.eq(2).height()])+$target.offset().top;
},
loadedTips:function(){
$('#loadTips').find('span').text('數(shù)據(jù)已加載完');
setTimeout(function(){
$('#loadTips').css({'visibility':'hidden'});
},200);
// 解綁事件
$(window).unbind('scroll',$.proxy(this.scrollEvent,this));
},
scrollEvent:function(){
if($(document).scrollTop()+$(window).height()>oTop&&on_off){
this.fillData();
}
},
init:function(){
this.fillData();
$(window).bind('scroll',$.proxy(this.scrollEvent,this));
}
}
}();
wallPic.init();
- Jquery實(shí)現(xiàn)瀑布流布局(備有詳細(xì)注釋)
- jquery代碼實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)圖片瀑布流效果
- jquery 插件實(shí)現(xiàn)瀑布流圖片展示實(shí)例
- jQuery實(shí)現(xiàn)瀑布流布局
- jQuery Masonry瀑布流插件使用詳解
- jQuery.lazyload+masonry改良圖片瀑布流代碼
- jQuery瀑布流插件Wookmark使用實(shí)例
- jquery簡(jiǎn)單瀑布流實(shí)現(xiàn)原理及ie8下測(cè)試代碼
- Jquery瀑布流插件使用介紹
- jQuery+HTML5美女瀑布流布局實(shí)現(xiàn)方法
相關(guān)文章
Jquery 數(shù)據(jù)選擇插件Pickerbox使用介紹
目前市面上很少見或幾乎沒有這數(shù)據(jù)(對(duì)象)選擇插件.比如,點(diǎn)擊input , select 元素時(shí)彈出div(窗口),載入數(shù)據(jù)讓用戶選擇數(shù)據(jù),選擇后在填充回對(duì)應(yīng)的元素.2012-08-08
javascript向后臺(tái)傳送相同屬性的參數(shù)即數(shù)組參數(shù)
在傳送參數(shù)時(shí),經(jīng)常會(huì)碰到向后臺(tái)傳送一些相同屬性的參數(shù),最好的選擇是采用數(shù)組的方式,下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-02-02
jQuery使用正則表達(dá)式限制文本框只能輸入數(shù)字
本文主要介紹jQuery中使用正則表達(dá)式限制文本框只能輸入數(shù)字的功能,希望能幫到大家,有需要的朋友可以參考一下。2016-06-06
用示例說明filter()與find()的用法以及children()與find()的區(qū)別分析
本篇文章介紹了,用示例說明filter()與find()的用法以及children()與find()的區(qū)別分析。需要的朋友參考下2013-04-04
JQuery和HTML5 Canvas實(shí)現(xiàn)彈幕效果
這篇文章主要介紹了JQuery和HTML5 Canvas兩種方法實(shí)現(xiàn)彈幕效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Jquery左右滑動(dòng)插件之實(shí)現(xiàn)超級(jí)炫酷動(dòng)畫效果附源碼下載
這是一款基于jquery.pogo-slider插件實(shí)現(xiàn)的多個(gè)滑塊切換特效。本篇文章給大家介紹Jquery左右滑動(dòng)插件之實(shí)現(xiàn)超級(jí)炫酷動(dòng)畫效果,對(duì)jquery左右滑動(dòng)插件相關(guān)知識(shí)感興趣的朋友參考下2015-12-12
jQuery實(shí)現(xiàn)帶水平滑桿的焦點(diǎn)圖動(dòng)畫插件
這是一款很實(shí)用的jQuery焦點(diǎn)圖動(dòng)畫插件,跟其他jQuery焦點(diǎn)圖插件不同的是,它帶有一個(gè)水平的滑桿,你可以通過滑動(dòng)滑桿實(shí)現(xiàn)圖片的切換,也可以通過點(diǎn)擊圖片來切換2016-03-03

