網(wǎng)頁(yè)瀑布流布局jQuery實(shí)現(xiàn)代碼
什么是瀑布流網(wǎng)頁(yè)布局?

瀑布流,又稱瀑布流式布局。是比較流行的一種網(wǎng)站頁(yè)面布局,視覺(jué)表現(xiàn)為參差不齊的多欄布局,隨著頁(yè)面滾動(dòng)條向下滾動(dòng),這種布局還會(huì)不斷加載數(shù)據(jù)塊并附加至當(dāng)前尾部。
下面來(lái)看代碼,用純CSS3來(lái)做看效果怎樣!
HTML
<div id="all"> <div class="box"> <div class="pic"> <img src="cars/1.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/2.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/3.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/4.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/5.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/6.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/7.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/8.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/9.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/10.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/11.jpg"/> </div> </div> <div class="box"> <div class="pic"> <img src="cars/12.jpg"/> </div> </div> </div>
這里用一個(gè)大盒子來(lái)裝全部?jī)?nèi)容,小盒子box裝塊內(nèi)容,pic盒子裝圖片??碿ss的代碼
CSS
*{
margin: 0;
padding: 0;
}
#all{
/*關(guān)鍵代碼*/
-webkit-column-width: 437px;
-moz-column-width: 437px;
-o-column-width: 437px;
-ms-column-width: 437px;
/*-webkit-column-count: 3;
-moz-column-count: 3;
-o-column-count: 3;
-ms-column-count: 3;*/
/*-webkit-column-rule: 2px dashed #F00;
-moz-column-rule: 2px dashed #F00;
-o-column-rule: 2px dashed #F00;
-ms-column-rule: 2px dashed #F00;*/
/*-webkit-column-gap: 5px;
-moz-column-gap: 5px;
-o-column-gap: 5px;
-ms-column-gap: 5px;*/
}
.box{
padding: 15px 0 0 15px;
}
.pic{
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 5px #ccc ;
width: 400px;
}
.pic>img{
width: 400px;
height: auto;
}
效果就出來(lái)了

可見(jiàn)CSS3雖然實(shí)現(xiàn)了瀑布流,但是畫風(fēng)看起來(lái)詭異,左右排布間距不夠靈活。列寬隨著瀏覽器窗口大小進(jìn)行改變,用戶體驗(yàn)不好,圖片排序按照垂直順序排列,打亂圖片顯示順序,圖片加載還是依靠JavaScript來(lái)實(shí)現(xiàn)。唯一優(yōu)勢(shì)是不需要計(jì)算,瀏覽器自動(dòng)計(jì)算,只需設(shè)置列寬,性能高。
為了得到更好的效果,所以我們還是用算法來(lái)實(shí)現(xiàn)吧,下面來(lái)看jquery代碼配合css來(lái)實(shí)現(xiàn)瀑布流。
CSS
*{
margin: 0;
padding: 0;
}
#all{
position: relative;
}
.box{
padding: 15px 0 0 15px;
float: left;
}
.pic{
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 5px #ccc ;
}
.pic>img{
width: 400px;
height: auto;
}
jquery
$(window).load(function(){
waterfall();
// var dataInt={"data":[{"src":"cars/1.jpg"},{"src":"cars/2.jpg"},{"src":"cars/3.jpg"},{"src":"cars/4.jpg"}]}
// $(window).scroll(function(){
// if(checkScrollSlide){
// $.each(dataInt.data,function(key,value){
// var oBox=$("<div>").addClass("box").appendTo($("#all"));
// var oPic=$("<div>").addClass("pic").appendTo($(oBox));
// var oImg=$("<img>").attr("src",$(value).attr("src")).appendTo($(oPic));
// })
// waterfall();
// }
// })
})
function waterfall(){
var $boxs=$("#all>div");
var w=$boxs.eq(0).outerWidth();
var cols=Math.floor($(window).width()/w);
$('#all').width(w*cols).css("margin","0 auto");
var hArr=[];
$boxs.each(function(index,value){
var h=$boxs.eq(index).outerHeight();
if(index<cols){
hArr[index]=h;
}else{
var minH=Math.min.apply(null,hArr);
var minHIndex=$.inArray(minH,hArr);
// console.log(minH);
$(value).css({
'position':'absolute',
'top':minH+'px',
'left':minHIndex*w+'px'
})
hArr[minHIndex]+=$boxs.eq(index).outerHeight();
}
});
}
//function checkScrollSlide(){
// var $lastBox=$("#all>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;
//}
效果如下

很明顯效果好多了,圖片排序是按照?qǐng)D片計(jì)算的位置橫向排序,位置是計(jì)算出來(lái)的,比較規(guī)范。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery Masonry瀑布流布局神器使用詳解
- jquery實(shí)現(xiàn)簡(jiǎn)單的瀑布流布局
- 基于jquery實(shí)現(xiàn)瀑布流布局
- jQuery實(shí)現(xiàn)瀑布流布局詳解(PC和移動(dòng)端)
- jQuery+HTML5美女瀑布流布局實(shí)現(xiàn)方法
- Jquery實(shí)現(xiàn)瀑布流布局(備有詳細(xì)注釋)
- jQuery實(shí)現(xiàn)瀑布流布局
- jQuery 瀑布流 絕對(duì)定位布局(二)(延遲AJAX加載圖片)
- jQuery 瀑布流 浮動(dòng)布局(一)(延遲AJAX加載圖片)
- JQuery實(shí)現(xiàn)簡(jiǎn)單瀑布流布局
相關(guān)文章
JQuery實(shí)現(xiàn)隱藏和顯示動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了JQuery實(shí)現(xiàn)隱藏和顯示動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
基于jQuery實(shí)現(xiàn)的旋轉(zhuǎn)彩圈實(shí)例
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)的旋轉(zhuǎn)彩圈,涉及jQuery定時(shí)操作頁(yè)面元素的相關(guān)技巧,需要的朋友可以參考下2015-06-06
PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
我們?cè)诒疚闹杏玫揭粋€(gè)Ajax表單提交插件:jqery.form.js,有高人修改了幾行代碼并改名為:jquery.wallform.js,直接拿來(lái)用。下面我們就來(lái)看看這款神奇的jQuery插件吧。2015-03-03
jquery實(shí)現(xiàn)導(dǎo)航固定頂部的效果仿蘑菇街
這篇文章主要介紹了jquery實(shí)現(xiàn)導(dǎo)航固定頂部的效果,仿蘑菇街的,感覺(jué)還不錯(cuò),下面將實(shí)例與大家分享下2014-10-10
jquery實(shí)現(xiàn)移動(dòng)端懸浮拖拽框
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)移動(dòng)端懸浮拖拽框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
jQuery實(shí)現(xiàn)鼠標(biāo)可拖動(dòng)調(diào)整表格列寬度
這篇文章主要介紹了通過(guò)jQuery實(shí)現(xiàn)鼠標(biāo)可拖動(dòng)調(diào)整表格列寬度,需要的朋友可以參考下2014-05-05
jQuery插件開發(fā)精品教程(讓你的jQuery更上一個(gè)臺(tái)階)
這篇jQuery插件開發(fā)教程是小編見(jiàn)過(guò)的最詳細(xì)的了,每個(gè)解說(shuō)都很好,對(duì)于想做增強(qiáng)插件的朋友確實(shí)不錯(cuò)的參考資料,特分享下,方便需要的朋友2015-11-11

