最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年10月21日 10:53:44   作者:測(cè)試貓  
這篇文章主要為大家詳細(xì)介紹了網(wǎng)頁(yè)瀑布流布局jQuery實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是瀑布流網(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

涟源市| 昌平区| 鄂托克旗| 兰溪市| 余庆县| 天气| 友谊县| 灌云县| 台州市| 武城县| 惠安县| 福鼎市| 河津市| 阿克苏市| 通榆县| 宜宾县| 望城县| 二连浩特市| 晋江市| 乐亭县| 榆社县| 石阡县| 万年县| 威宁| 江北区| 黑河市| 荔波县| 泰宁县| 乌鲁木齐县| 镇坪县| 武陟县| 黔西| 四川省| 台中市| 平和县| 黑水县| 桦川县| 邹城市| 文水县| 新泰市| 永平县|