javascript实现瀑布流动态加载图片原理
本文实例为大家分享了js瀑布流加载效果,动态加载图片,供大家参考,具体内容如下
鼠标滚动事件,当鼠标滚动到下边,动态加载图片。
1.HTML代码
<!DOCTYPEhtml> <html> <head> <metacharset="UTF-8"> <title>js实现瀑布流效果-动态加载图片</title> <linkrel="stylesheet"href="css/waterfallflow.css"type="text/css"/> <scriptsrc="js/waterfallflow.js"></script> </head> <body> <divid="container"> <divclass="box"> <divclass="box_img"> <imgsrc="img/1.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/2.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/3.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/4.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/5.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/6.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/7.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/3.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/1.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/2.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/1.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/2.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/3.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/4.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/5.jpg"/> </div> </div> <divclass="box"> <divclass="box_img"> <imgsrc="img/6.jpg"/> </div> </div> </div> </body> </html>
2.CSS代码
*{ margin:0px; padding:0px; } #container{ position:relative; } .box{ padding:5px; float:left; margin:0pxauto; } .box_img{ padding:5px; border:1pxsolid#DCDCDC; box-shadow:005px#ccc; border-radius:5px; } .box_imgimg{ width:230px; }
3.JavaScript代码
window.onload=function(){ imgLocation("container","box"); varimgData={"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()){ varcparent=document.getElementById("container"); for(vari=0;i<imgData.data.length;i++){ varccontent=document.createElement("div"); ccontent.className="box"; cparent.appendChild(ccontent); varboximg=document.createElement("div"); boximg.className="box_img"; ccontent.appendChild(boximg); varimg=document.createElement("img"); img.src="img/"+imgData.data[i].src; boximg.appendChild(img); //另外一种方法在div后边追加内容,不覆盖原有内容 //varcontent="<divclass='box'><divclass='box_img'><imgsrc='img/"+imgData.data[i].src+"'/></div></div>"; //cparent.innerHTML+=content; } imgLocation("container","box"); } } } functioncheckFlag(){ varcparent=document.getElementById("container"); varccontent=getChildElement(cparent,"box");//图片的所有box数 varlastContentHeight=ccontent[ccontent.length-1].offsetTop;//最后一张图片距离顶部高度 varscrollTop=document.documentElement.scrollTop||document.body.scrollTop;//滚动条距离顶部高度 varpageHeight=document.documentElement.clientHeight||document.body.clientHeight;//屏幕高度 //console.log(lastContentHeight+","+scrollTop+","+pageHeight); if(lastContentHeight<scrollTop+pageHeight){ returntrue; } } functionimgLocation(parent,content){ varcparent=document.getElementById(parent); varccontent=getChildElement(cparent,content);//图片的所有box数 varimgWidth=ccontent[0].offsetWidth;//图片宽度 varnum=Math.floor(document.documentElement.clientWidth/imgWidth);//一行放图片个数 cparent.style.cssText="width:"+imgWidth*num+"px;margin:0pxauto";//container的宽度 varboxHeightArr=[];//每一列box高度 for(vari=0;i<ccontent.length;i++){ if(i<num){ boxHeightArr[i]=ccontent[i].offsetHeight; }else{ varminHeight=Math.min.apply(null,boxHeightArr);//最小高度 varminIndex=getMinheightLocation(boxHeightArr,minHeight);//得到最小高度下标 //console.log(minHeight+","+minIndex); ccontent[i].style.position="absolute"; ccontent[i].style.top=minHeight+"px";//距离顶部高度 ccontent[i].style.left=ccontent[minIndex].offsetLeft+"px";//距离左边长度 boxHeightArr[minIndex]+=ccontent[i].offsetHeight; //console.log(ccontent[i].offsetHeight+","+ccontent[i].height);//ccontent[i].height=undefined } } } functiongetMinheightLocation(boxHeightArr,minHeight){ for(variinboxHeightArr){ if(boxHeightArr[i]==minHeight){ returni } } } functiongetChildElement(parent,content){ //将parent下有content的全部取出 varcontentArr=[]; varallContent=parent.getElementsByTagName("*"); for(i=0;i<allContent.length;i++){ if(allContent[i].className=="box"){ contentArr.push(allContent[i]); } } returncontentArr; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。