瀑布流的实现方式(原生js+jquery+css3)(3)

var arrBox=getClassObj(parentID,childClass);// getClassObj()获取子class的数组 var iBoxW=arrBox[0].offsetWidth;// 获取瀑布流块的宽度 var num=Math.floor(document.documentElement.clientWidth/iBoxW);//计算窗口能容纳几列 oParent.style.width=iBoxW*num+'px';//设置父级宽度

6.每设置一块位移,都要在列高的数组上增加数值,防止块重叠 

arrBox[i].style.position='absolute';//设置绝对位移 arrBox[i].style.top=minH+'px'; arrBox[i].style.left=minHIndex*iBoxW+'px';//也可以直接获取arrBox[minHIndex].offsetLeft arrBoxH[minHIndex]+=arrBox[i].offsetHeight;//添加后,更新最小列高

b.jquery
 1.思路是跟js一样的,只是jquery封装了很多方法,让我们简便的就实现了
 2.注意width(),跟innerWidth()的区别。前者只能获取宽度值(不包括补白padding) 

css3版本

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>瀑布流-css3</title> <style> *{margin:0;padding:0;} #content{margin:0 auto;position: relative;width:1200px;column-count:6;-moz-column-count:6;-webkit-column-count:6;} .box{padding:10px;width: 180px;} .box img{width: 180px;height:auto;display: block;} </style> <script> window.onload=function(){ //如果数据不够,没出现滚动条,自动加载数据 var time=setInterval(function(){ if(checkscrollside()){ addDate();//插入数据 }else{ clearInterval(time); window.onscroll=function(){ if(checkscrollside()){ addDate(); }; } } },1000) } // 数据插入 function addDate(){ var dataInt=['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg'];//模拟数据,也可以是对象 var oParent = document.getElementById('content'); for(var i=0;i<dataInt.length;i++){//循环插入数据 var oBox=document.createElement('div'); oBox.className='box'; oParent.appendChild(oBox); var oImg=document.createElement('img'); oImg.src='https://www.jb51.net/article/img/'+dataInt[i]; oBox.appendChild(oImg); } } //获取子class的数组 function getClassObj(parentID,childClass){ var oParent=document.getElementById(parentID); var allChildObj=oParent.getElementsByTagName('*');//获取父级下的所有子集 var childObj=[];//创建一个数组 用于收集子元素 for (var i=0;i<allChildObj.length;i++) {//遍历子元素、判断类别、压入数组 if (allChildObj[i].className==childClass){ childObj.push(allChildObj[i]); } }; return childObj; } // 判断滚动条是否到底部 function checkscrollside(){ var arrBox=getClassObj("content",'box'); //获取最后一个瀑布流块的高度:距离网页顶部(实现未滚到底就开始加载) var lastBoxH=arrBox[arrBox.length-1].offsetTop; var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//获取滚动条卷走的高度 var documentH=document.documentElement.clientHeight;//显示页面文档的高 return (lastBoxH<scrollTop+documentH)?true:false;//到达指定高度后 返回true,触发waterfall()函数 } </script> </head> <body> <div> <div><img src="https://www.jb51.net/img/0.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/1.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/2.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/3.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/4.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/5.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/6.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/7.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/8.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/9.jpg" alt=""></div> <div><img src="https://www.jb51.net/img/10.jpg" alt=""></div> </div> </body> </html>

注意点

1.滚动加载还是得另外加js 
2.加载的数据,是竖向排列的。体验不是很友好 
3.有兼容性问题,Internet Explorer 10 +

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzswjg.html