html部分(圖片都是本地,自己需要改動圖片) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" type="text/css" href="css/one.css" ...
html部分(圖片都是本地,自己需要改動圖片)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/one.css"/>
<script type="text/javascript" src="js/script.js" ></script>
</head>
<body>
<!--瀑布流的特點是等寬不等高-->
<!--怎麼滑都沒有盡頭-->
<div id="main">
<div class="box">
<div class="pic">
<img src="img/003.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/005.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/006.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/007.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/009.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/010.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/011.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/012.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/013.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/014.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/016.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/018.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/019.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/020.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/021.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/022.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/023.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/024.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/025.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/index.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/index001.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/u=1265768299,3970469844&fm=21&gp=0.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/022.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/023.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/024.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/009.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/010.jpg"/>
</div>
</div>
<div class="box">
<div class="pic">
<img src="img/011.jpg"/>
</div>
</div>
</div>
</body>
</html>
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: 0 0 5px #ccc;
}
.pic img{
width: 300px;
height: auto;
}
js部分
window.onload = function() {
waterfall('main', 'box');
var dataInt={"data":[{"src":'003.jpg'},{"src":'022.jpg'},{"src":'025.jpg'},{"src":'006.jpg'},{"src":'007.jpg'},{"src":'019.jpg'},{"src":'020.jpg'},{"src":'010.jpg'}]}
window.onscroll=function(){
if (checkScrollSlide) {
var oParent=document.getElementById("main");
//將數據塊渲染到當頁面的尾部
for (var i=0;i<dataInt.data.length;i++) {
var oBox=document.createElement('div');
oBox.className='box';
oParent.appendChild(oBox);
var oPic=document.createElement('div');
oPic.className='pic';
oBox.appendChild(oPic);
var oImg=document.createElement('img');
oImg.src="img/"+dataInt.data[i].src;
oPic.appendChild(oImg);
}
waterfall('main', 'box');
}
}
}
function waterfall(parent, box) {
//將main下的所有的class為box的元素取出來
var oParent = document.getElementById(parent);
var oBoxs = getByClass(oParent, box);
// console.log(oBoxs.length);
//計算整個頁面顯示的列數(頁面寬/box的寬)
var oBoxw = oBoxs[0].offsetWidth; //獲得每一列的寬度
// console.log(oBoxw);
//獲取頁面的寬度除以每一列的寬度
var cols = Math.floor(document.documentElement.clientWidth / oBoxw);
//console.log(cols);
//設置main的寬
oParent.style.cssText = 'width:' + oBoxw * cols + 'px;margin:0 auto';
//聲明一個數組,存放每一列的高度
var hArr = [];
//遍歷所有的oBoxs
for(var i = 0; i < oBoxs.length; i++) {
if(i < cols) {
hArr.push(oBoxs[i].offsetHeight);
} else {
//求第一列box的最小的高
//藉助apply方法改變函數中this的指向,就是可以對數組取最小值
var minH = Math.min.apply(null, hArr);
var index=getMinhIndex(hArr,minH);//索引數組中高最小的那個
oBoxs[i].style.position='absolute';
oBoxs[i].style.top=minH+'px';
//oBoxs[i].style.left=oBoxw*index+'px';
oBoxs[i].style.left=oBoxs[index].offsetLeft+'px';
hArr[index]+=oBoxs[i].offsetHeight;
}
}
console.log(hArr);
}
//根據class獲取元素
function getByClass(parent, clsName) {
var boxArr = new Array(); //用來存儲獲取到的所有的class為box的元素
oElements = parent.getElementsByTagName('*'); //獲得main下麵的所有的元素,形成一個數組
//遍曆數組oElements
for(var i = 0; i < oElements.length; i++) {
if(oElements[i].className == clsName) {
boxArr.push(oElements[i]);
}
}
return boxArr;
}
//getMinhIndex(hArr,minH);
//找到數組中最小數值的arr[i]
function getMinhIndex(arr,val){
for(var i in arr){
if (arr[i]==val) {//數組hArr[i]中的minH是最小值,此時的i就是我們想要的
return i;
}
}
}
//檢測是否具備了滾動條載入數據塊的條件
function checkScrollSlide(){
var oParent=document.getElementById('main');
var oBoxs=getByClass(oParent,'box');
var lastBoxH=oBoxs[oBoxs.length-1].offsetTop+
Math.floor(oBoxs[oBoxs.length-1].offsetHeight/2);
var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;
// console.log(scrollTop);
var height=document.body.clientHeight||document.documentElement.clientHeight;
// console.log(height);
return (lastBoxH<scrollTop+height)?true:false;
}