javascript实现瀑布流动态加载图片原理

网络编程 2021-07-04 20:00www.168986.cn编程入门
这篇文章主要为大家详细介绍了javascript实现瀑布流动态加载图片原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了js瀑布流加载效果,动态加载图片,供大家参考,具体内容如下

鼠标滚动事件,当鼠标滚动到下边,动态加载图片。

1. HTML代码    

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>js实现瀑布流效果-动态加载图片</title>
  <link rel="stylesheet" href="css/waterfallflow.css" type="text/css" />
  <script src="js/waterfallflow.js"></script>
 </head>
 <body>
  <div id="container">
   <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 class="box_img">
     <img src="img/5.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/3.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/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 class="box_img">
     <img src="img/5.jpg" />
    </div>
   </div>
   <div class="box">
    <div class="box_img">
     <img src="img/6.jpg" />
    </div>
   </div>
    
  </div>
 </body>
</html>

2. CSS代码    

{
 margin: 0px;
 padding: 0px;
}
#container{
 position: relative;
}
.box{
 padding: 5px;
 float: left;
 margin: 0px auto;
}
.box_img{
 padding: 5px;
 border: 1px solid #DCDCDC;
 box-shadow: 0 0 5px #c;
 border-radius: 5px;
}
.box_img img{
 width: 230px;
}

3. JavaScript代码    

window.onload=function(){
  
 imgLocation("container","box");
 var imgData={"data":[{"src":"1.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"},{"src":"6.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"}]}
 window.onscroll=function(){
//  console.log(document.documentElement.scrollTop);
  if(checkFlag()){
   var cparent=document.getElementById("container");
   for(var i=0;i<imgData.data.length;i++){
    var ontent=document.createElement("div");
    ontent.className="box";
    cparent.appendChild(ontent);
    var boximg=document.createElement("div");
    boximg.className="box_img";
    ontent.appendChild(boximg);
    var img=document.createElement("img");
    img.src="img/"+imgData.data[i].src;
    boximg.appendChild(img);
     
    //一种方法在div后边追加内容,不覆盖原有内容
//    var content="<div class='box'><div class='box_img'><img src='img/"+imgData.data[i].src+"'/></div></div>";
//    cparent.innerHTML+=content;
   }
   imgLocation("container","box");
  }
 }
}
 
function checkFlag(){
 var cparent=document.getElementById("container");
 var ontent=getChildElement(cparent,"box");//图片的所有box数
 var lastContentHeight=ontent[ontent.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 ontent=getChildElement(cparent,content);//图片的所有box数
 var imgWidth=ontent[0].offsetWidth;//图片宽度
 var num=Math.floor(document.documentElement.clientWidth/imgWidth);//一行放图片个数
 cparent.style.cssText="width:"+imgWidthnum+"px;margin:0px auto";//container的宽度
 var boxHeightArr=[];//每一列box高度
 for(var i=0;i<ontent.length;i++){
  if(i<num){
   boxHeightArr[i]=ontent[i].offsetHeight;
  }else{
   var minHeight=Math.min.apply(null,boxHeightArr);//最小高度
   var minIndex = getMinheightLocation(boxHeightArr,minHeight);//得到最小高度下标
//   console.log(minHeight+","+minIndex);
   ontent[i].style.position="absolute";
   ontent[i].style.=minHeight+"px";//距离顶部高度
   ontent[i].style.left=ontent[minIndex].offsetLeft+"px";//距离左边长度
   boxHeightArr[minIndex]+=ontent[i].offsetHeight;
//   console.log(ontent[i].offsetHeight+","+ontent[i].height);//ontent[i].height=undefined
    
  }
 }
}
 
function getMinheightLocation(boxHeightArr,minHeight){
 for(var i in boxHeightArr){
  if(boxHeightArr[i]==minHeight){
   return i
  }
 }
}
 
function getChildElement(parent,content){
 //将parent下有content的全部取出
 var contentArr=[];
 var allContent=parent.getElementsByTagName("");
 for(i=0;i<allContent.length;i++){
  if(allContent[i].className=="box"){
   contentArr.push(allContent[i]);
  }
 }
 return contentArr;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by