怎么基于JavaScript實現(xiàn)無限加載瀑布流

這篇文章主要介紹怎么基于JavaScript實現(xiàn)無限加載瀑布流,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站主營梁河網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,重慶APP開發(fā),梁河h5小程序定制開發(fā)搭建,梁河網(wǎng)站營銷推廣歡迎梁河等地區(qū)企業(yè)咨詢

本文實例為大家分享了JS實現(xiàn)無限加載瀑布流展示的具體代碼,供大家參考,具體內(nèi)容如下

1.在外層的div中,插入4個ul,ul左浮動
2.每次在創(chuàng)建節(jié)點之后,插入節(jié)點之前,要獲取每個ul的高度,找到最小的高度,然后將新創(chuàng)建的li節(jié)點插入該ul中
3.當文檔的高度 - 文檔的可視高度 <= 鼠標的滑動距離時 開始繼續(xù)創(chuàng)建節(jié)點

代碼:

<!DOCTYPE html> 
<html> 
 
  <head> 
    <meta charset="UTF-8"> 
    <title>瀑布流效果動態(tài)加載</title> 
    <style type="text/css"> 
      * { 
        margin: 0; 
        padding: 0; 
      } 
       
      #content { 
        width: 1000px; 
        border: 3px solid red; 
        margin: 0 auto; 
        overflow: hidden; 
      } 
       
      #content > ul { 
        width: 240px; 
        padding: 4px; 
        border: 1px solid blue; 
        float: left; 
        list-style-type: none; 
      } 
       
      #content > ul > li { 
        background-color: yellow; 
        font-size: 100px; 
        color: white; 
        text-align: center; 
        margin-bottom: 5px; 
      } 
    </style> 
  </head> 
 
  <body> 
    <div id="content"> 
      <ul></ul> 
      <ul></ul> 
      <ul></ul> 
      <ul></ul> 
    </div> 
  </body> 
  <script type="text/javascript"> 
    //隨機[m,n]之間的整數(shù) 
    function randomNumber(m, n) { 
      return Math.floor(Math.random() * (n - m + 1) + m); 
    } 
    //隨機顏色 
    function randomColor() { 
      return "rgb(" + randomNumber(0, 255) + "," + randomNumber(0, 255) + "," + randomNumber(0, 255) + ")"; 
    } 
    //獲取當前的scrollTop 
    var scrollTopDistance; 
    //獲取所有的ul 
    var uls = document.getElementsByTagName("ul"); 
    var i = 0; 
    var k = i; 
    function waterFall(){ 
      for (i = k;i < k + 4;i++) { 
        //創(chuàng)建li 
        var li = document.createElement("li"); 
        //隨機顏色 
        li.style.backgroundColor = randomColor(); 
        //隨機高度 
        li.style.height = randomNumber(150, 500) + "px"; 
        //手動轉(zhuǎn)換為字符串 
        li.innerHTML = i + 1 + ""; 
        //插入到對應的ul中 
        //判斷哪個ul的高度低,該次創(chuàng)建的li就插入到此ul中 
        var index = 0; //記錄下標 
        for (var j = 0; j < uls.length; j++) { 
          if (uls[j].offsetHeight < uls[index].offsetHeight) { 
            index = j; 
          } 
        } 
        //將元素節(jié)點插入文檔中 
        uls[index].appendChild(li); 
      } 
      k = i; 
      return uls[index].offsetHeight; 
    } 
    waterFall(); 
    var height; 
    //無限加載瀑布流方法,核心思想就是 
    document.onmousewheel = function(){ 
      //文檔的高度 - 文檔的可視高度 < 鼠標的滑動距離 時開始加載圖片 
      height = document.body.clientHeight - document.documentElement.clientHeight; 
      if(height <= document.body.scrollTop){ 
        waterFall(); 
      } 
    } 
  </script> 
</html>

以上是“怎么基于JavaScript實現(xiàn)無限加載瀑布流”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章標題:怎么基于JavaScript實現(xiàn)無限加載瀑布流
網(wǎng)站路徑:http://muchs.cn/article32/gphppc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設動態(tài)網(wǎng)站、做網(wǎng)站小程序開發(fā)、品牌網(wǎng)站制作、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化