本篇内容实现类似百度图片的呈现功能,瀑布流+自动加载
index13.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>uvi</title>
<link rel="stylesheet" href="style2.css" type="text/css">
<script src="myjs.js"></script>
</head>
<body>
<div id="container">
<div class="box">
<div class="box_img">
<img src="img.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img2.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img3.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img4.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img5.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img6.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img7.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img8.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img9.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img3.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img4.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img5.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img6.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img7.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img8.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img9.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img3.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img4.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img5.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img6.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img7.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img8.jpg">
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img9.jpg">
</div>
</div> </div>
</div> </body>
</html>
css代码:
*{
margin:0px;
padding:0px;
}
#container{
position:relative; }
.box{
padding: 5px;
float: left;
}
.box_img{
padding:5px;
border:1px solid #cccccc;
box-shadow: 5px #cccccc;
border-radius:5px;
}
.box_img img{
width:1000px;
height:auto;
}
js代码
window.onload = function(){
imgLocation("container","box");
var imgData={"data":[{"src":"img.jpg"},{"src":"img2.jpg"},{"src":"img3.jpg"}]};
window.onscroll = function(){//监听滚动条
if(checkFlag()){
var cparent = document.getElementById("container");
for(var i=;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 = 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-].offsetTop;//获取最后一张照片距离顶部的高度
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
var pageHeight = document.documentElement.clientHeight||document.body.clientHeight;
if(lastContentHeight<scrollTop+pageHeight){
return true;
} } function imgLocation(parent,content){
//将parent下的所有content全部取出
var cparent = document.getElementById(parent);
var ccontent = getChildElement(cparent,content);
var imgWidth = ccontent[].offsetWidth;
var num =Math.floor(document.documentElement.clientWidth/imgWidth);
cparent.style.cssText = "width:"+imgWidth*num+"px;margin:0px auto"; var BoxHeightArr = [];
for(var i = ;i<ccontent.length;i++){
if(i<num){
BoxHeightArr[i] = ccontent[i].offsetHeight;
}else{
var minHeight = Math.min.apply(null,BoxHeightArr);//获得最小高度
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;
}
}
} 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 =;i<allcontent.length;i++){
if(allcontent[i].className==content){
contentArr.push(allcontent[i]);
}
}
return contentArr;
}