js瀑布流布局

來源:互聯網
上載者:User

標籤:style   io   ar   for   sp   div   on   問題   cti   

     絕大多數的網頁都是分為幾列進行排版(幾個div),傳統布局就是將內容模組直接寫在這幾個div中。但是這種布局存在一個問題,那就是如果這個頁面的資訊非常的龐大,內容很多時這幾列div會非常的長。而瀏覽器是逐行載入的,上述情況下會出現頁面一直在載入第一列的內容,半天載入不完,而另外的幾列什麼都不顯示。

使用者是橫向觀看網頁的,而上述情況下網頁卻是在逐列的縱向載入,這使得使用者體驗極差。為解決這種情況,就需要瀑布流布局。在幾列中進行橫向載入。

代碼如下:

function fnRand(min,max){
  return parseInt(Math.random()*(max-min)+min);
}
window.onload=function (){

  var oWrap=document.getElementById("wrap");
  var aUl=oWrap.children;

  function createLi(){
    var oLi=document.createElement("li");
    oLi.style.height=fnRand(100,300)+"px";

    var ul1=aUl[0].offsetHeight;
    var ul2=aUl[1].offsetHeight;
    var ul3=aUl[2].offsetHeight;

    var min=Math.min(ul1,ul2,ul3);                   //找到幾列中高度最矮的 高度值

    for (var j = 0; j < aUl.length; j++) {
      if (min==aUl[j].offsetHeight){               //通過高度值來找出最矮的列
        min=aUl[j];
      }
    }
    min.appendChild(oLi);                        //將新的內容放入最矮的列
  }

  for (var i = 0; i < 20; i++) {
    createLi();
  }
  window.onscroll=function (){

    var bodyScrollTop=document.documentElement.scrollTop||document.body.scrollTop;

    var max=oWrap.offsetHeight-document.documentElement.clientHeight;

    if (bodyScrollTop==max){
      alert("到底了");
      for (var i = 0; i < 20; i++) {        //到底了再繼續載入
        createLi();
      }
    }
  }
}

js瀑布流布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.