瀑布流无限加载(原生js)

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Title</title>  
  6.     <link rel="stylesheet" href="css/initialize.css">  
  7.     <style>  
  8.         .out {  
  9.             position: relative;  
  10.             margin: 0 auto;  
  11.         }  
  12.   
  13.         .in {  
  14.             float: left;  
  15.         }  
  16.   
  17.         .img {  
  18.             margin: 10px;  
  19.             padding: 10px;  
  20.             border-radius: 10px;  
  21.             border: 1px solid #666;  
  22.             box-shadow: 0 0 5px #ff7ba5;  
  23.   
  24.         }  
  25.     </style>  
  26. </head>  
  27. <body>  
  28. <div class="out">  
  29.     <div class="in">  
  30.         <div class="img"><img src="img/i1.jpg" alt=""></div>  
  31.     </div>  
  32.     <div class="in">  
  33.         <div class="img"><img src="img/i2.jpg" alt=""></div>  
  34.     </div>  
  35.     <div class="in">  
  36.         <div class="img"><img src="img/i3.jpg" alt=""></div>  
  37.     </div>  
  38.     <div class="in">  
  39.         <div class="img"><img src="img/i4.jpg" alt=""></div>  
  40.     </div>  
  41.     <div class="in">  
  42.         <div class="img"><img src="img/i5.jpg" alt=""></div>  
  43.     </div>  
  44.     <div class="in">  
  45.         <div class="img"><img src="img/i6.jpg" alt=""></div>  
  46.     </div>  
  47.     <div class="in">  
  48.         <div class="img"><img src="img/i7.jpg" alt=""></div>  
  49.     </div>  
  50.     <div class="in">  
  51.         <div class="img"><img src="img/i8.jpg" alt=""></div>  
  52.     </div>  
  53.     <div class="in">  
  54.         <div class="img"><img src="img/i9.jpg" alt=""></div>  
  55.     </div>  
  56.     <div class="in">  
  57.         <div class="img"><img src="img/i10.jpg" alt=""></div>  
  58.     </div>  
  59.     <div class="in">  
  60.         <div class="img"><img src="img/i11.jpg" alt=""></div>  
  61.     </div>  
  62.     <div class="in">  
  63.         <div class="img"><img src="img/i12.jpg" alt=""></div>  
  64.     </div>  
  65.     <div class="in">  
  66.         <div class="img"><img src="img/i13.jpg" alt=""></div>  
  67.     </div>  
  68.     <div class="in">  
  69.         <div class="img"><img src="img/i14.jpg" alt=""></div>  
  70.     </div>  
  71.     <div class="in">  
  72.         <div class="img"><img src="img/i15.jpg" alt=""></div>  
  73.     </div>  
  74.     <div class="in">  
  75.         <div class="img"><img src="img/i3.jpg" alt=""></div>  
  76.     </div>  
  77.     <div class="in">  
  78.         <div class="img"><img src="img/i4.jpg" alt=""></div>  
  79.     </div>  
  80.     <div class="in">  
  81.         <div class="img"><img src="img/i5.jpg" alt=""></div>  
  82.     </div>  
  83.     <div class="in">  
  84.         <div class="img"><img src="img/i6.jpg" alt=""></div>  
  85.     </div>  
  86.     <div class="in">  
  87.         <div class="img"><img src="img/i7.jpg" alt=""></div>  
  88.     </div>  
  89.     <div class="in">  
  90.         <div class="img"><img src="img/i8.jpg" alt=""></div>  
  91.     </div>  
  92.     <div class="in">  
  93.         <div class="img"><img src="img/i9.jpg" alt=""></div>  
  94.     </div>  
  95.     <div class="in">  
  96.         <div class="img"><img src="img/i10.jpg" alt=""></div>  
  97.     </div>  
  98. </div>  
  99.   
  100. <script>  
  101.     window.onload = function () {  
  102.         var data = '{"data":[{"src":"i1.jpg"},{"src":"i2.jpg"},{"src":"i3.jpg"},{"src":"i4.jpg"},{"src":"i5.jpg"},{"src":"i6.jpg"},{"src":"i7.jpg"},{"src":"i8.jpg"},{"src":"i9.jpg"},{"src":"i10.jpg"},{"src":"i11.jpg"},{"src":"i12.jpg"},{"src":"i13.jpg"},{"src":"i14.jpg"},{"src":"i15.jpg"}]}';  
  103.         var out = document.getElementsByClassName('out')[0];  
  104.         var inName = document.getElementsByClassName('in');  
  105.         function pbl() {  
  106.             var arr = [];  
  107.             var winWid = document.documentElement.clientWidth;  
  108.             var imgWid = inName[0].offsetWidth;  
  109.             num = Math.floor(winWid / imgWid);  
  110.             if(num<1) return;  
  111.             out.style.width = num * imgWid + 'px';  
  112.             for (var i = 0; i < inName.length; i++) {  
  113.                 if (i < num) {  
  114.                     arr.push(inName[i].offsetHeight);  
  115.                     inName[i].style.position = 'static';  
  116.                 } else {  
  117.                     inName[i].style.position = 'absolute';  
  118.                     var minArr = Math.min.apply(null, arr);  
  119.                     var index = arr.indexOf(minArr);  
  120.                     inName[i].style.top = minArr + 'px';  
  121.                     arr[index] += inName[i].offsetHeight;  
  122.                     inName[i].style.left = inName[index].offsetLeft + 'px';  
  123.                 }  
  124.             }  
  125.         }  
  126.         pbl();  
  127.         window.onscroll = function () {  
  128.             var widH = document.documentElement.clientHeight; //获取时候的高度  
  129.             var tTop = inName[inName.length - 1].offsetTop; //获取最后一张图片到顶部的距离  
  130.             var hdH = document.documentElement.scrollTop||document.body.scrollTop; //获取滑动的高度  
  131.             if(tTop<widH+hdH){  
  132.                 var jieGuo = JSON.parse(data);  
  133.                 for(var i=0;i<jieGuo.data.length;i++){  
  134.                     var div1 = document.createElement('div');  
  135.                     div1.className = 'in';  
  136.                     var div2 = document.createElement('div');  
  137.                     div2.className = 'img';  
  138.                     var img = new Image;  
  139.                     img.src = 'img/'+jieGuo.data[i].src;  
  140.                     div2.appendChild(img);  
  141.                     div1.appendChild(div2);  
  142.                     out.appendChild(div1);  
  143.                 }  
  144.                 pbl();  
  145.             }  
  146.         };  
  147.         window.onresize = function () {  
  148.             pbl();  
  149.         }  
  150.     };  
  151.   
  152.   
  153.   
  154. </script>  
  155. </body>  
  156. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值