用js实现瀑布流代码,实现下拉过程中图片的自动加载:
效果如下:
![]98]ZI}Y[~1]WE9OFBV}}VA.png](https://img.haomeiwen.com/i2618457/922595fae5cfca8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
简易目录:
O2AVO_B7(`L4]MB{`6KO4@F.png下边是js代码:
window.onload = function(){
imgLocation('container','box');
var imgData = {"data":[{'src':"02.jpg"},{'src':"03.jpg"},{'src':"05.jpg"},{'src':"06.jpg"}]}
window.onscroll = 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 boxing = document.createElement('div');
boxing.className = "img_box";
ccontent.appendChild(boxing);
var img = document.createElement('img');
img.src = "images/" + imgData.data[i].src;
boxing.appendChild(img);
}
imgLocation('container','box');
}
}
}
function checkFlag(){
var cparent = document.getElementById('container');
var ccontent = getChildElement(cparent,"box");
var lastContentHeight = ccontent[ccontent.length-1].offsetTop;
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
var pageHeight = document.documentElement.clientHeight||document.body.clientHeight;
//console.log(lastContentHeight+":"+scrollTop+":"+pageHeight);
if(lastContentHeight<scrollTop+pageHeight){
return true;
}
}
function imgLocation(parent,content){
var cparent = document.getElementById(parent);
var ccontent = getChildElement(cparent,content);
var imgWidth = ccontent[0].offsetWidth;
var cols = Math.floor(document.documentElement.clientWidth / imgWidth);
cparent.style.cssText = "width:"+imgWidth*cols+"px;margin:0 auto;";
var BoxHeightArr = [];
for(var i=0;i<ccontent.length;i++){
if(i<cols){
BoxHeightArr[i] = ccontent[i].offsetHeight;
}else{
var minheight = Math.min.apply(null,BoxHeightArr);
var minIndex = getimiheightLocation(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 getimiheightLocation(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;
}
下边是css代码:
*{
margin:0px;
padding:0px;
}
#container{
position: relative;
}
.box{
padding: 5px;
float: left;
}
.img_box{
padding: 5px;
border:1px solid #cccccc;
border-radius: 5px;
box-shadow: 0 0 5px #ccc;
}
.img_box img{
width:150px;
height:auto;
}
下边是静态页面代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<script type="text/javascript" src="app.js"></script>
<body>
<div id="container">
<div class="box">
<div class="img_box">
![](images/01.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/02.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/03.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/04.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/05.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/06.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/07.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/08.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/09.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/10.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/11.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/12.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/13.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/14.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/15.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/16.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/17.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/18.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/19.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/20.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/21.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/22.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/23.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/24.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/25.jpg)
</div>
</div>
<div class="box">
<div class="img_box">
![](images/26.jpg)
</div>
</div>
</div>
</body>
</html>
网友评论