本文實例為大家分享了js瀑布流布局學習資料,供大家參考,具體內容如下
特點:等寬不等高。
實現方式:Javascript/Jquery/CSS3多欄布局。
樣例網站:花瓣網-->分類
一、JS實現瀑布流
index.html:頁面結構
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>瀑布流布局</title> <link rel="stylesheet" href="styles/layout.css"> </head> <body> <div id="main"> <div class="box"><div class="pic"><img src="../waterFall/pic/0.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/1.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/2.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/3.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/4.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/5.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/6.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/7.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/8.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/9.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/10.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/11.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/12.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/13.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/14.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/15.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/16.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/17.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/18.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/19.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/20.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/21.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/22.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/23.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/24.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/25.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/26.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/27.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/28.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/29.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/30.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/31.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/32.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/33.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/34.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/35.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/36.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/37.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/38.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/39.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/40.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/41.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/42.jpg" alt=""></div></div> <div class="box"><div class="pic"><img src="../waterFall/pic/43.jpg" alt=""></div></div> </div> <script src="scripts/waterfall.js"></script> </body> </html>
layout.css:頁面元素樣式
*{ pdding:0; margin:0; } div#main{ position: relative; } div.box{ padding:15px 0 0 15px; float: left; } div.pic{ padding:10px; border:1px solid #ccc; border-radius:5px; box-shadow: 0 0 5px #ccc; } .pic img{ height:auto; width:165px; }
waterfall.js
window.onload=function(){ waterFall('main','box'); //模擬後台相應數據json var dataInt={ "data": [ {"src":"0.jpg"}, {"src":"1.jpg"}, {"src":"2.jpg"}, {"src":"3.jpg"}, {"src":"4.jpg"}, {"src":"5.jpg"}, {"src":"6.jpg"} ] } window.onscroll=function(){ if(checkScrollSlide){ //將數據塊渲染到當前頁面的尾部 var oParent=document.getElementById("main"); for(var i=0;i<dataInt.data.length;i++){ var oBox=document.createElement("div"); oBox.className="box"; oParent.appendChild(oBox); var oPic=document.createElement("div"); oPic.className="pic"; oBox.appendChild(oPic); var img=document.createElement("img"); img.setAttribute("src",dataInt.data[i]); img.src="pic/"+dataInt.data[i].src; oPic.appendChild(img); } dataInt=null;//清空數據塊,防止無限加載 waterFall('main','box');//對頁面新元素進行布局渲染 } } } function waterFall(parent,box){ //將main下的class為box的所有元素取出來 var oParent=document.getElementById(parent); var oBoxs=getByClass(oParent,box); console.log(oBoxs.length); //計算整個頁面顯示的列數(頁面寬/box寬) var oBoxW=oBoxs[0].offsetWidth; // console.log(oBoxW); var cols=Math.floor(document.documentElement.clientWidth/oBoxW); // console.log(cols); //設置main的寬 oParent.style.cssText="width:"+oBoxW*cols+"px;margin:0 auto;" var hArr=[];//存放每列高度的數組 for(var i=0;i<oBoxs.length;i++){ if(i<cols){ hArr.push(oBoxs[i].offsetHeight); }else{ var minH=Math.min.apply(null,hArr);//獲取當前數組最小高度值 // console.log(minH); var index=getMinhIndex(hArr,minH);//獲取數組最小高度的索引 //console.log(index); oBoxs[i].style.position="absolute";//將之後的圖片依次絕對定位 oBoxs[i].style.top=minH+"px"; oBoxs[i].style.left=index*oBoxW+"px";//計算新圖片所在的位置並賦值 hArr[index]+=oBoxs[i].offsetHeight;//變化數組列的高度值,因為加上了一張圖片 } //console.log(hArr); } } //根據class獲取元素 function getByClass(parent,clsName){ var boxArr=new Array(),//用來存儲獲取到的所有class為box的元素 oElements=parent.getElementsByTagName("*"); for(var i=0;i<oElements.length;i++){ if(oElements[i].className==clsName){ boxArr.push(oElements[i]); } } return boxArr; } function getMinhIndex(arr,val){ for(var i=0;i<arr.length;i++){ if(arr[i]==val){ return i; } } } //檢測是否具備滾動條加載數據塊條件 function checkScrollSlide(){ var oParent=document.getElementById("main"); var oBoxs=getByClass(oParent,"box"); var lastBoxH=oBoxs[oBoxs.length-1].offsetTop+Math.floor(oBoxs[oBoxs.length-1].offsetHeight/2); var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;//混雜模式和標准模式下的scrollTop獲取 //console.log(scrollTop); var height=document.body.clientHeight||document.documentElement.clientHeight;//混雜模式和標准模式下的浏覽器窗口高度獲取 return (lastBoxH<scrollTop+height)?true:false;//檢測最後一個box高度是否小於滾動高度+窗口高度,返回布爾值 }
二、JQuery
$( window ).on( "load", function(){ waterfall('main','pin'); var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]}; window.onscroll=function(){ if(checkscrollside()){ $.each( dataInt.data, function( index, value ){ var $oPin = $('<div>').addClass('pin').appendTo( $( "#main" ) ); var $oBox = $('<div>').addClass('box').appendTo( $oPin ); $('<img>').attr('src','./images/' + $( value).attr( 'src') ).appendTo($oBox); }); waterfall(); }; } }); /* parend 父級id pin 元素id */ function waterfall(parent,pin){ var $aPin = $( "#main>div" ); var iPinW = $aPin.eq( 0 ).width();// 一個塊框pin的寬 var num = Math.floor( $( window ).width() / iPinW );//每行中能容納的pin個數【窗口寬度除以一個塊框寬度】 //oParent.style.cssText='width:'+iPinW*num+'px;ma rgin:0 auto;';//設置父級居中樣式:定寬+自動水平外邊距 $( "#main" ).css({ 'width:' : iPinW * num, 'margin': '0 auto' }); var pinHArr=[];//用於存儲 每列中的所有塊框相加的高度。 $aPin.each( function( index, value ){ var pinH = $aPin.eq( index ).height(); if( index < num ){ pinHArr[ index ] = pinH; //第一行中的num個塊框pin 先添加進數組pinHArr }else{ var minH = Math.min.apply( null, pinHArr );//數組pinHArr中的最小值minH var minHIndex = $.inArray( minH, pinHArr ); $( value ).css({ 'position': 'absolute', 'top': minH + 15, 'left': $aPin.eq( minHIndex ).position().left }); //數組 最小高元素的高 + 添加上的aPin[i]塊框高 pinHArr[ minHIndex ] += $aPin.eq( index ).height() + 15;//更新添加了塊框後的列高 } }); } function checkscrollside(){ var $aPin = $( "#main>div" ); var lastPinH = $aPin.last().get(0).offsetTop + Math.floor($aPin.last().height()/2);//創建【觸發添加塊框函數waterfall()】的高度:最後一個塊框的距離網頁頂部+自身高的一半(實現未滾到底就開始加載) var scrollTop = $( window ).scrollTop()//注意解決兼容性 var documentH = $( document ).height();//頁面高度 return (lastPinH < scrollTop + documentH ) ? true : false;//到達指定高度後 返回true,觸發waterfall()函數 }
三、CSS多欄布局
.container{ -webkit-column-width:160px; -moz-column-width:160px; -webkit-column-gap:5px; -moz-column-gap:5px; } /*數據塊 磚塊*/ .container div{width:160px; margin:4px 0;}
【css3和js實現方法比較】
--css3方式--
1:不需要計算,浏覽器自動計算,只需設置1列寬,性能高
2:列寬隨著浏覽器寬口大小進行改變,用戶體驗不好;
3:圖片排序按照垂直順序排列,打亂圖片顯示順序
4.圖片加載還是需要js
--js方式--
js實現的瀑布流不會有上面的缺點,但是性能相對要差!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。