jQuery實(shí)現(xiàn)瀑布流布局
HTML
<div id="main">
<div class="box">
<div class="pic">
<img src="images/0.jpg" alt="">
</div>
</div>
<div class="box">
<div class="pic">
<img src="images/1.jpg" alt="">
</div>
</div>
<div class="box">
<div class="pic">
<img src="images/2.jpg" alt="">
</div>
</div>
<div class="box">
<div class="pic">
<img src="images/3.jpg" alt="">
</div>
</div>
<div class="box">
<div class="pic">
<img src="images/4.jpg" alt="">
</div>
</div>
<div class="box">
<div class="pic">
<img src="images/2.jpg" alt="">
</div>
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
#main {
position: relative;
}
.box {
padding:15px 0 0 15px;
float:left;
}
.pic {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0px 0px 5px #ccc;
img {
width:165px;
height:auto;
}
}
JavaScript
$(window).on("load",function () {
waterfall();
var dataInt = { "data":[{"src":"7.jpg"},{"src":"8.jpg"},{"src":"9.jpg"},{"src":"6.jpg"}]}
//模擬json數(shù)據(jù);
$(window).on("scroll",function () {
if(checkScrollSlide){
$.each(dataInt.data,function (key,value) {
var oBox=$("<div>").addClass("box").appendTo($("#main"));
//jQuery支持連綴,隱式迭代;
var oPic=$("<div>").addClass('pic').appendTo($(oBox));
$("<img>").attr("src","images/"+$(value).attr("src")).appendTo(oPic);
});
waterfall();
}
})
});
//流式布局主函數(shù);
function waterfall () {
var $boxs=$("#main>div");
//獲取#main元素下的直接子元素div.box;
//獲取每一列的寬度;
var w=$boxs.eq(0).outerWidth();
//outerWidth()獲取包含padding和border在內(nèi)的寬度;
//var w=$boxs.eq(0).width();
//width()只能獲取給元素定義的寬度;
var cols=Math.floor($(window).width()/w);
//獲取多少列;
$("#main").width(w*cols).css("margin","0 auto");
//設(shè)置#main元素的寬度和居中樣式;
var hArr=[];
//每一列高度的集合;
$boxs.each(function (index,value) {
//遍歷每一個(gè)box元素;
//為了找到之前所有元素的最低點(diǎn),然后將本元素設(shè)置到最低點(diǎn)之下;
var h=$boxs.eq(index).outerHeight();
//每一個(gè)box元素的高,
if (index<cols) {
hArr[index]=h;
//確定每列第一個(gè)元素的高度;
} else{
var minH=Math.min.apply(null,hArr);
//得出列高數(shù)組中的最小高度;
var minHIndex=$.inArray(minH,hArr);
//$.inArray()方法得出元素(minH)在數(shù)組(hArr)中的index值;
//console.log(value);
//此時(shí)的value是第一行之后的所有的box元素的DOM對(duì)象!;
$(value).css({
//$(value):將DOM對(duì)象轉(zhuǎn)換成jQuery對(duì)象,才能繼續(xù)使用jQuery方法;
"position":"absolute",
"top":minH+"px",
"left":minHIndex*w+"px"
});
hArr[minHIndex]+=$boxs.eq(index).outerHeight();
//最低高元素的高度+剛添加到最低高度下的元素的高度=新的列高;
};
});
// console.log(hArr);
};
function checkScrollSlide () {
var $lastBox=$("#main>div").last();
var lastBoxDis=$lastBox.offset().top+Math.floor($lastBox.outerHeight()/2);
var scrollTop=$(window).scrollTop();
var documentH=$(window).height();
return (lastBoxDis<scrollTop+documentH)?true:false;
}
詳細(xì)解釋清仔細(xì)參考注釋吧,我就不單獨(dú)再拉出來寫了。
- jQuery Masonry瀑布流布局神器使用詳解
- jquery實(shí)現(xiàn)簡(jiǎn)單的瀑布流布局
- 網(wǎng)頁瀑布流布局jQuery實(shí)現(xiàn)代碼
- 基于jquery實(shí)現(xiàn)瀑布流布局
- jQuery實(shí)現(xiàn)瀑布流布局詳解(PC和移動(dòng)端)
- jQuery+HTML5美女瀑布流布局實(shí)現(xiàn)方法
- Jquery實(shí)現(xiàn)瀑布流布局(備有詳細(xì)注釋)
- jQuery 瀑布流 絕對(duì)定位布局(二)(延遲AJAX加載圖片)
- jQuery 瀑布流 浮動(dòng)布局(一)(延遲AJAX加載圖片)
- JQuery實(shí)現(xiàn)簡(jiǎn)單瀑布流布局
相關(guān)文章
jQuery/JS監(jiān)聽input輸入框值變化實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于jQuery/JS監(jiān)聽input輸入框值變化實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10jquery插件開發(fā)之實(shí)現(xiàn)jquery手風(fēng)琴功能分享
需要一個(gè)手風(fēng)琴效果,就動(dòng)手寫了一個(gè)。其實(shí)有多個(gè)現(xiàn)成的jQuery手風(fēng)琴插件可以用,但對(duì)比了一下,總感覺有些笨重,還是自己寫的脈絡(luò)自己最清楚,擴(kuò)展起來也更容易些2014-03-03jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能【附php后臺(tái)接收】
這篇文章主要介紹了jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能,結(jié)合實(shí)例形式分析了jQuery+ajax結(jié)合layer.js插件與php后臺(tái)交互,實(shí)現(xiàn)顯示上傳進(jìn)度的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06jquery和js實(shí)現(xiàn)對(duì)div的隱藏和顯示方法
jquery和js分別可以實(shí)現(xiàn)對(duì)div的隱藏和顯示,方法也不一樣,jquery的show、show;js的hidden、visible2014-09-09jquery動(dòng)態(tài)添加以及遍歷option并獲取特定樣式名稱的option方法
下面小編就為大家分享一篇jquery動(dòng)態(tài)添加以及遍歷option并獲取特定樣式名稱的option方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01JQuery 進(jìn)入頁面默認(rèn)給已賦值的復(fù)選框打鉤
這篇文章主要介紹了JQuery 進(jìn)入頁面默認(rèn)給已賦值的復(fù)選框打鉤的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-03-03jQuery 錨點(diǎn)跳轉(zhuǎn)滾動(dòng)條平滑滾動(dòng)一句話代碼
jQuery 錨點(diǎn)跳轉(zhuǎn)滾動(dòng)條平滑滾動(dòng)一句話代碼,需要的朋友可以參考下。2010-04-04