一与境、Html布局
<div id="main">
<div class="box">
<div class="pic">
![](images.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](mingo.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](ace.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](images.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](mingo.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](ace.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](mingo.jpg)
</div>
</div>
<div class="box">
<div class="pic">
![](ace.jpg)
</div>
</div>
</div>
二兰珍、Css樣式
<style>
*{
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:0 0 5px #ccc;
}
.pic img{
width: 320px;
height: auto;
}
</style>
三洞拨、Js部分
$(window).on('load',function(){
waterfall();
$(window).on('scroll',function(){
var dataInt = {"data":[{"src":"images.jpg"},{"src":"ace.jpg"},{"src":"mingo.jpg"}]};
if(checkScrollSide){
$.each(dataInt.data,function(key,val){
var oBox = $('<div>').addClass('box').appendTo($('#main'));
var oPic = $('<div>').addClass('pic').appendTo($(oBox));
$('<img>').attr("src",$(val).attr('src')).appendTo($(oPic));
})
waterfall();
}
})
})
function waterfall(){
var $boxs = $("#main>div");//找到main下面的圖片盒子
var w = $boxs.eq(0).outerWidth();//圖片盒子寬
var cols = Math.floor($(window).width()/w);//列數(shù)
$("main").width(w*cols).css("margin","0 auto");
var hArr = [];
$boxs.each(function(index,val) {
console.log(val)
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);//獲取高度最低的圖片盒子的索引值
$value = $(val);//將Dom對(duì)象轉(zhuǎn)成jQuery對(duì)象
$value.css({
'position':'absolute',
'top':minH+'px',
'left':minHIndex*w + 'px'
})
hArr[minHIndex] += $boxs.eq(index).outerHeight();
}
});
}
function checkScrollSide(){
var $lastBox = $("#main>div").last();
var laseBoxDis = $lastBox.offset().top+Math.floor($lastBox.outerHeight()/2);
var scrollTop = $(window).scrollTop();
var documentH = $(window).height();
return (lastBoxDis < scrollTop+documentH)?true:false;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者