如何用JS實(shí)現(xiàn)網(wǎng)頁(yè)瀑布流布局
瀑布流 又稱(chēng)瀑布流式布局,是比較流行的一種網(wǎng)站頁(yè)面布局方式。即多行等寬元素排列,后面的元素依次添加到其后,等寬不等高,根據(jù)圖片原比例縮放直至寬度達(dá)到我們的要求,依次按照規(guī)則放入指定位置。
什么是瀑布流布局:先看效果:
不完整html代碼:
<div id='container'><div class='box'> <div class='box-img'><img src='http://www.hdgsjgj.cn/bcjs/img/1.jpg' alt=''> </div></div><div class='box'> <div class='box-img'><img src='http://www.hdgsjgj.cn/bcjs/img/2.jpg' alt=''> </div></div><div class='box'> <div class='box-img'><img src='http://www.hdgsjgj.cn/bcjs/img/3.jpg' alt=''> </div></div> </div> ......<!-- 省略了圖片,多少?gòu)垐D片自行決定-->
完整的css代碼
*{padding: 0;margin: 0; } #container{position: relative; } .box{float: left;padding: 15px; } .box-img {width: 150px;padding: 5px;border: 1px solid #ccc ;box-shadow: 0 0 5px #ccc;border-radius: 5px; } .box-img img{width: 100%;height: auto; }如何實(shí)現(xiàn):
簡(jiǎn)單地來(lái)說(shuō),如果要實(shí)現(xiàn)瀑布流布局,得完成這幾件事✍
1. 獲取圖片function getChildElemnt() { const contentArr = []//定義數(shù)組準(zhǔn)備裝圖 const parent = document.getElementById(container)//得到整個(gè)頁(yè)面 const allContent = parent.getElementsByTagName(’*’)//得到整個(gè)標(biāo)簽 console.log(allContent); for (var i = 0; i < allContent.length; i++) { if (allContent[i].className == ’box’) {contentArr.push(allContent[i])//將class=’box’的標(biāo)簽裝入數(shù)組 } } console.log(contentArr); return contentArr//返回?cái)?shù)組 }2. 設(shè)置圖片寬帶
var ccontent = getChildElemnt() var imgWidth = ccontent[0].offsetWidth//令所有圖片寬度等于第一張圖片3. 計(jì)算瀏覽器頁(yè)面一行最多能存放圖片的數(shù)量
var dWidth=document.documentElement.clientWidth//頁(yè)面寬度var num = Math.floor(dWidth/ imgWidth)//Math.floor()向下取整4. 比較圖片高度
因?yàn)樵谄俨剂鞑季种校?dāng)?shù)谝恍袌D片已經(jīng)擺滿(mǎn)后,第二行的第一張圖片要放在第一行中高度最小的圖片的下面
var BoxHeightArr = []//定義一個(gè)數(shù)組,把每張圖片的高度依次放進(jìn)去 for (var i = 0; i < ccontent.length; i++) { if (i < num) {BoxHeightArr[i] = ccontent[i].offsetHeight//將圖片的高度存入數(shù)組 } else {//當(dāng)?shù)谝恍幸呀?jīng)存放不了圖片后var minHeight = Math.min.apply(null, BoxHeightArr)//比較出上一行最小的高度 } }5. 得到上一行中最小高度圖片的位置
//定義一個(gè)getMinHeightLocation函數(shù),給它傳入BoxHeightArr上一行全部圖片,和minHeight上一行圖片的最小高度 function getMinHeightLocation(BoxHeightArr, minHeight) { for (var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) {//當(dāng)圖片高度等于最小高度時(shí),該圖片的位置為最小高度圖片的位置return i } } }6. 插圖
for (var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply(null, BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = ’absolute’//將要插入的圖片絕對(duì)定位,即元素的位置通過(guò) 'left', 'top', 'right' 以及 'bottom' 屬性進(jìn)行規(guī)定 ccontent[i].style.top = minHeight + ’px’//令插入的圖片到頂端的距離剛好等于要插其下面圖片的高度 ccontent[i].style.left = ccontent[minIndex].offsetLeft + ’px’//令插入的圖片到最左邊的距離剛好等于要插其下面圖片到最左邊的距離 BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight//插入圖片后,得將這位置的高度設(shè)為兩張圖片的高度和 } }完整代碼如下:
優(yōu)化代碼,提高性能
window.onload = function() { imgLocation(’container’, ’box’)//構(gòu)造函數(shù)imgLocation}//用window.onload = function() {}函數(shù)就不用等著body頁(yè)面中調(diào)用就可以執(zhí)行了// 獲取到當(dāng)前有多少?gòu)垐D片要擺放function imgLocation(parent, content) {//令parent=’container’,content=’box’ // 將parent下所有的內(nèi)容全部取出 var cparent = document.getElementById(parent) var ccontent = getChildElemnt(cparent, content) var imgWidth = ccontent[0].offsetWidth var num = Math.floor(document.documentElement.clientWidth / imgWidth) cparent.style.cssText = `width: ${imgWidth * num} px` var BoxHeightArr = [] for (var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply(null, BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = ’absolute’ ccontent[i].style.top = minHeight + ’px’ ccontent[i].style.left = ccontent[minIndex].offsetLeft + ’px’ BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight } } // console.log(BoxHeightArr);}function getChildElemnt(parent, content) {parent=’container’,content=’box’ const contentArr = [] const allContent = parent.getElementsByTagName(’*’) console.log(allContent); for (var i = 0; i < allContent.length; i++) { if (allContent[i].className == content) { contentArr.push(allContent[i]) } } console.log(contentArr); return contentArr}function getMinHeightLocation(BoxHeightArr, minHeight) { for (var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) { return i } }}
以上就是如何用JS實(shí)現(xiàn)網(wǎng)頁(yè)瀑布流布局的詳細(xì)內(nèi)容,更多關(guān)于JS實(shí)現(xiàn)網(wǎng)頁(yè)瀑布流布局的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. XML在語(yǔ)音合成中的應(yīng)用2. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))3. 不要在HTML中濫用div4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)5. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別6. jscript與vbscript 操作XML元素屬性的代碼7. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)8. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)9. ASP基礎(chǔ)入門(mén)第四篇(腳本變量、函數(shù)、過(guò)程和條件語(yǔ)句)10. XML入門(mén)的常見(jiàn)問(wèn)題(三)
