由于沒(méi)有服務(wù)器,就用一個(gè)json字符串作為下拉時(shí)加載圖片數(shù)據(jù)的來(lái)源了
html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="js/waterfall.js"></script>
<style type="text/css">
*{padding: 0;margin:0;}
#main{
position: relative;
}
.pin{
padding: 15px 0 0 15px;
float:left;
}
.box{
padding: 10px;
border:1px solid #ccc;
box-shadow: 0 0 6px #ccc;
border-radius: 5px;
}
.box img{
width:162px;
height:auto;
}
</style>
</head>
<body>
<div id="main">
<div class="pin">
<div class="box">
<img src="./images/1.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/2.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/3.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/4.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/5.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/6.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/7.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/8.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/9.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/10.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/11.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/12.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/13.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/14.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/15.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/16.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/17.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/18.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/19.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/20.jpg"/>
</div>
</div>
<div class="pin">
<div class="box">
<img src="./images/21.jpg"/>
</div>
</div>
</div>
</body>
</html>
JSWallper.js:
window.onload=function(){
waterfall('main','pin');
var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]};
window.onscroll=function(){
if(checkscrollside()){
var oParent = document.getElementById('main');// 父級(jí)對(duì)象
for(var i=0;i<dataInt.data.length;i++){
var oPin=document.createElement('div'); //添加 元素節(jié)點(diǎn)
oPin.className='pin'; //添加 類名 name屬性
oParent.appendChild(oPin); //添加 子節(jié)點(diǎn)
var oBox=document.createElement('div');
oBox.className='box';
oPin.appendChild(oBox);
var oImg=document.createElement('img');
oImg.src='./images/'+dataInt.data[i].src;
oBox.appendChild(oImg);
}
waterfall('main','pin');
};
}
}
/*
parend 父級(jí)id
pin 元素id
*/
function waterfall(parent,pin){
var oParent=document.getElementById(parent);// 父級(jí)對(duì)象
var aPin=getClassObj(oParent,pin);// 獲取存儲(chǔ)塊框pin的數(shù)組aPin
var iPinW=aPin[0].offsetWidth;// 一個(gè)塊框pin的寬
var num=Math.floor(document.documentElement.clientWidth/iPinW);//每行中能容納的pin個(gè)數(shù)【窗口寬度除以一個(gè)塊框?qū)挾取?/P>
oParent.style.cssText='width:'+iPinW*num+'px;margin:0 auto;';//設(shè)置父級(jí)居中樣式:定寬+自動(dòng)水平外邊距
var pinHArr=[];//用于存儲(chǔ) 每列中的所有塊框相加的高度。a
for(var i=0;i<aPin.length;i++){//遍歷數(shù)組aPin的每個(gè)塊框元素
var pinH=aPin[i].offsetHeight;
if(i<num){
pinHArr[i]=pinH; //第一行中的num個(gè)塊框pin 先添加進(jìn)數(shù)組pinHArr
}else{
var minH=Math.min.apply(null,pinHArr);//數(shù)組pinHArr中的最小值minH
var minHIndex=getminHIndex(pinHArr,minH);
aPin[i].style.position='absolute';//設(shè)置絕對(duì)位移
aPin[i].style.top=minH+'px';
aPin[i].style.left=aPin[minHIndex].offsetLeft+'px';
//數(shù)組 最小高元素的高 + 添加上的aPin[i]塊框高
pinHArr[minHIndex]+=aPin[i].offsetHeight;//更新添加了塊框后的列高
}
}
}
/*
*通過(guò)父級(jí)和子元素的class類 獲取該同類子元素的數(shù)組
*/
function getClassObj(parent,className){
var obj=parent.getElementsByTagName('*');//獲取 父級(jí)的所有子集
var pinS=[];//創(chuàng)建一個(gè)數(shù)組 用于收集子元素
for (var i=0;i<obj.length;i++) {//遍歷子元素、判斷類別、壓入數(shù)組
if (obj[i].className==className){
pinS.push(obj[i]);
}
};
return pinS;
}
/****
*獲取 pin高度 最小值的索引index
*/
function getminHIndex(arr,minH){
for(var i in arr){
if(arr[i]==minH){
return i;
}
}
}
function checkscrollside(){
var oParent=document.getElementById('main');
var aPin=getClassObj(oParent,'pin');
var lastPinH=aPin[aPin.length-1].offsetTop+Math.floor(aPin[aPin.length-1].offsetHeight/2);//創(chuàng)建【觸發(fā)添加塊框函數(shù)waterfall()】的高度:最后一個(gè)塊框的距離網(wǎng)頁(yè)頂部+自身高的一半(實(shí)現(xiàn)未滾到底就開(kāi)始加載)
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//注意解決兼容性
var documentH=document.documentElement.clientHeight;//頁(yè)面高度
return (lastPinH<scrollTop+documentH)?true:false;//到達(dá)指定高度后 返回true,觸發(fā)waterfall()函數(shù)
}