原生JavaScript實現瀑布流布局_javascript技巧

來源:互聯網
上載者:User

本文執行個體講解了原生JavaScript實現瀑布流布局詳細代碼,分享給大家供大家參考,具體內容如下

效果圖:

具體代碼:

window.onload=function(){  waterfall('main','pin');  var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]};    window.onscroll=function(){    if(checkscrollside()){      var oParent = document.getElementById('main');// 父級對象      for(var i=0;i<dataInt.data.length;i++){        var oPin=document.createElement('div'); //添加 元素節點        oPin.className='pin';          //添加 類名 name屬性        oParent.appendChild(oPin);       //添加 子節點        var oBox=document.createElement('div');        oBox.className='box';        oPin.appendChild(oBox);        var oImg=document.createElement('img');        oImg.src='./images/'+dataInt.data[i].src;        oBox.appendChild(oImg);      }      waterfall('main','pin');    };  }}/*  parend 父級id  pin 元素class*/function waterfall(parent,pin){  var oParent=document.getElementById(parent);// 父級對象  var aPin=getClassObj(oParent,pin);// 擷取儲存塊框pin的數組aPin  var iPinW=aPin[0].offsetWidth;// 一個塊框pin的寬  var num=Math.floor(document.documentElement.clientWidth/iPinW);//每行中能容納的pin個數【視窗寬度除以一個塊框寬度】  oParent.style.cssText='width:'+iPinW*num+'px;margin:0 auto;';//設定父級置中樣式:定寬+自動水平外邊距  var pinHArr=[];//用於儲存 每列中的所有塊框相加的高度。  for(var i=0;i<aPin.length;i++){//遍曆數組aPin的每個塊框元素    var pinH=aPin[i].offsetHeight;    if(i<num){      pinHArr[i]=pinH; //第一行中的num個塊框pin 先添加進數組pinHArr    }else{      var minH=Math.min.apply(null,pinHArr);//數組pinHArr中的最小值minH      var minHIndex=getminHIndex(pinHArr,minH);      aPin[i].style.position='absolute';//設定絕對位移      aPin[i].style.top=minH+'px';      aPin[i].style.left=aPin[minHIndex].offsetLeft+'px';      //數組 最小高元素的高 + 添加上的aPin[i]塊框高      pinHArr[minHIndex]+=aPin[i].offsetHeight;//更新添加了塊框後的列高    }  }}/*  通過父級和子項目的class類 擷取該同類子項目的數組*/function getClassObj(parent,className){  var obj=parent.getElementsByTagName('*');//擷取 父級的所有子集  var pinS=[];//建立一個數組 用於收集子項目  for (var i=0;i<obj.length;i++) {//遍曆子項目、判斷類別、壓入數組    if (obj[i].className==className){      pinS.push(obj[i]);    }  };  return pinS;}/*  擷取 pin高度 最小值的索引index*/function getminHIndex(arr,minH){  for(var i in arr){    if(arr[i]==minH){      return i;    }  }}/*  判斷載入圖片的條件*/function checkscrollside(){  var oParent=document.getElementById('main');  var aPin=getClassObj(oParent,'pin');  var lastPinH=aPin[aPin.length-1].offsetTop+Math.floor(aPin[aPin.length-1].offsetHeight/2);//建立【觸發添加塊框函數waterfall()】的高度:最後一個塊框的距離網頁頂部+自身高的一半(實現未滾到底就開始載入)  var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//注意解決相容性  var documentH=document.documentElement.clientHeight;//頁面高度  return (lastPinH<scrollTop+documentH)?true:false;//到達指定高度後 返回true,觸發waterfall()函數}

以上就是本文的全部內容,希望對大家實現瀑布流布局有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.