唉呀妈呀,头一次写这么多前端代码==就是实现一个图片密铺,滚动条下拉且一直加载(类似于百度图片搜索界面的效果)
图片都是存储在img文件夹下的 js存在js文件夹下 css存在css文件夹下
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>瀑布流</title>
<script src="js/app.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="container">
<div class="box">
<div class="box_img">
<img src="img/0.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/1.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/2.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/3.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/4.jpg">
</div>
</div>
<div class="box">
<div box_img>
<img src="img/5.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/0.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/6.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/7.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/8.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/9.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/10.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/11.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/9.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/12.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/13.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/14.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/15.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/16.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/17.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/18.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/19.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/20.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/21.jpg">
</div>
</div>
</div>
</body>
</html>
window.οnlοad=function(){
imgLocation("container","box");
var imgData={"data":[{"src":"2.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"7.jpg"},{"src":"8.jpg"}]}
window.οnscrοll= function () {
if(checkFlag())
{
var cparent=document.getElementById("container");
for(var i=0;i<imgData.data.length;i++)
{
var ccontent=document.createElement("div");
ccontent.className="box";
cparent.appendChild(ccontent);
var boximg=document.createElement("div");
boximg.className="box_img";
ccontent.appendChild(boximg);
var img=document.createElement("img");
img.src="img/"+imgData.data[i].src;
boximg.appendChild(img);
}
imgLocation("container","box");
}
}
}
function checkFlag(){
var cparent=document.getElementById("container");
var ccontent=getChildElement(cparent,"box");
var lastContentHeight=ccontent[ccontent.length-1].offsetTop;
//console.log(lastContentHeight);
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
// console.log(scrollTop);
var pageHeight=document.documentElement.clientHeight||document.body.clientHeight;
//console.log(lastContentHeight+":"+scrollTop+":"+pageHeight);
if(lastContentHeight<scrollTop+pageHeight){
return true;
}
else return false;
}
function imgLocation(parent,content){
var cparent=document.getElementById(parent);
var ccontent=getChildElement(cparent,content);
//console.log(ccontent);
var imgWidth=ccontent[0].offsetWidth;
var num=Math.floor(document.documentElement.clientWidth/imgWidth);
cparent.style.cssText="width:"+imgWidth*num+"px;margin:0 auto";
var BoxHeightArr=[];
for(var i=0;i<ccontent.length;i++)
{
if(i<num)
{
BoxHeightArr[i]=ccontent[i].offsetHeight;
}
else
{
var minHeight=Math.min.apply(null,BoxHeightArr);
//console.log(minHeight);
var minIndex=getminheightLocation(BoxHeightArr,minHeight);
ccontent[i].style.position="absolute";
ccontent[i].style.top=minHeight+"px";
ccontent[i].style.left=ccontent[minIndex].offsetLeft+"px";
BoxHeightArr[minIndex]=BoxHeightArr[minIndex]+ccontent[i].offsetHeight;
}
// console.log(BoxHeightArr[i]);
}
}
function getminheightLocation(BoxHeightArr,minHeight)
{
for(var i in BoxHeightArr)
{
if(BoxHeightArr[i]==minHeight)
return i;
}
}
function getChildElement(parent,content){
var contentArr=[];
var allcontent=parent.getElementsByTagName("*");
for(var i=0;i<allcontent.length;i++)
{
if(allcontent[i].className==content)
{
contentArr.push(allcontent[i]);
}
}
return contentArr;
}
*{//class
margin:0px;
padding:0px;
}
#container{//id
position: relative;
}
.box{
padding :5px;
float:left;
}
.box_img{
padding: 5px;
border:1px solid #cccccc;
box_shadow:0 0 5px #cccccc;
border_radius:5px;
}
.box_img img{
weight:150px;
height:auto;
}