實(shí)現(xiàn)瀑布流布局的三種方式
前言
今天逛閑魚(yú)的時(shí)候觀察到每一行的高度不是相同的,經(jīng)了解才知道原來(lái)這是一種瀑布流布局,感覺(jué)挺有意思,于是決定研究一下,在網(wǎng)上也找了一些方案,實(shí)現(xiàn)瀑布流大概有3種方式。
一、JS 實(shí)現(xiàn)瀑布流
思路分析
- 瀑布流布局的特點(diǎn)是等寬不等高。
- 為了讓最后一行的差距最小,從第二行開(kāi)始,需要將圖片放在第一行最矮的圖片下面,以此類(lèi)推。
- 父元素設(shè)置為相對(duì)定位,圖片所在元素設(shè)置為絕對(duì)定位。然后通過(guò)設(shè)置 top 值和 left 值定位每個(gè)元素。
代碼實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100%;
position:relative;
}
.item {
position: absolute;
}
.item img{
width: 100%;
height:100%;
}
</style>
</head>
<body>
<div class="box">
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
</div>
</body>
<script src="jquery.min.js"></script>
<script>
function waterFall() {
// 1 確定圖片的寬度 - 滾動(dòng)條寬度
var pageWidth = getClient().width-8;
var columns = 3; //3列
var itemWidth = parseInt(pageWidth/columns); //得到item的寬度
$(".item").width(itemWidth); //設(shè)置到item的寬度
var arr = [];
$(".box .item").each(function(i){
var height = $(this).find("img").height();
if (i < columns) {
// 2 第一行按序布局
$(this).css({
top:0,
left:(itemWidth) * i+20*i,
});
//將行高push到數(shù)組
arr.push(height);
} else {
// 其他行
// 3 找到數(shù)組中最小高度 和 它的索引
var minHeight = arr[0];
var index = 0;
for (var j = 0; j < arr.length; j++) {
if (minHeight > arr[j]) {
minHeight = arr[j];
index = j;
}
}
// 4 設(shè)置下一行的第一個(gè)盒子位置
// top值就是最小列的高度
$(this).css({
top:arr[index]+30,//設(shè)置30的距離
left:$(".box .item").eq(index).css("left")
});
// 5 修改最小列的高度
// 最小列的高度 = 當(dāng)前自己的高度 + 拼接過(guò)來(lái)的高度
arr[index] = arr[index] + height+30;//設(shè)置30的距離
}
});
}
//clientWidth 處理兼容性
function getClient() {
return {
width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
}
}
// 頁(yè)面尺寸改變時(shí)實(shí)時(shí)觸發(fā)
window.onresize = function() {
//重新定義瀑布流
waterFall();
};
//初始化
window.onload = function(){
//實(shí)現(xiàn)瀑布流
waterFall();
}
</script>
</html>
效果如下

二、column 多行布局實(shí)現(xiàn)瀑布流
思路分析:
- column 實(shí)現(xiàn)瀑布流主要依賴兩個(gè)屬性。
- 一個(gè)是 column-count 屬性,是分為多少列。
- 一個(gè)是 column-gap 屬性,是設(shè)置列與列之間的距離。
代碼實(shí)現(xiàn):
<!DOCTYPE html>
<html>
<head>
<style>
.box {
margin: 10px;
column-count: 3;
column-gap: 10px;
}
.item {
margin-bottom: 10px;
}
.item img{
width: 100%;
height:100%;
}
</style>
</head>
<body>
<div class="box">
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
</div>
</body>
效果如下:

三、flex 彈性布局實(shí)現(xiàn)瀑布流
思路分析:
flex 實(shí)現(xiàn)瀑布流需要將最外層元素設(shè)置為 display: flex,即橫向排列。然后通過(guò)設(shè)置 flex-flow:column wrap 使其換行。設(shè)置 height: 100vh 填充屏幕的高度,來(lái)容納子元素。每一列的寬度可用 calc 函數(shù)來(lái)設(shè)置,即 width: calc(100%/3 - 20px)。分成等寬的 3 列減掉左右兩遍的 margin 距離。
代碼實(shí)現(xiàn):
<!DOCTYPE html>
<html>
<head>
<style>
.box {
display: flex;
flex-flow:column wrap;
height: 100vh;
}
.item {
margin: 10px;
width: calc(100%/3 - 20px);
}
.item img{
width: 100%;
height:100%;
}
</style>
</head>
<body>
<div class="box">
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="show.jpg" alt="" />
</div>
<div class="item">
<img src="cloth.jpg" alt="" />
</div>
<div class="item">
<img src="banner.jpg" alt="" />
</div>
</div>
</body>
效果如下:

四、3種方式對(duì)比
如果只是簡(jiǎn)單的頁(yè)面展示,可以使用 column 多欄布局和 flex 彈性布局。如果需要?jiǎng)討B(tài)添加數(shù)據(jù),或者動(dòng)態(tài)設(shè)置列數(shù),就需要使用到 JS + jQuery。
以上所述是小編給大家介紹的實(shí)現(xiàn)瀑布流布局的三種方式,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
js獲取GridView中行數(shù)據(jù)的兩種方法 分享
這篇文章介紹了js獲取GridView中行數(shù)據(jù)的方法,有需要的朋友可以參考一下2013-07-07
JS監(jiān)聽(tīng)變量改變的實(shí)現(xiàn)
本文主要介紹了JS監(jiān)聽(tīng)變量改變的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
js?fill函數(shù)填充數(shù)組或?qū)ο蟮慕鉀Q方法
這篇文章主要介紹了js?fill函數(shù)填充數(shù)組或?qū)ο蟮膯?wèn)題及解決方法,本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
js createRange與createTextRange的一些用法實(shí)例
關(guān)于createTextRange和createRange的一些用法,腳本之家增強(qiáng)版。2010-05-05
用JavaScript獲取頁(yè)面文檔內(nèi)容的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇用JavaScript獲取頁(yè)面文檔內(nèi)容的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表定義與使用方法示例
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表定義與使用方法,簡(jiǎn)單介紹了雙向鏈表的原理,并結(jié)合實(shí)例形式分析了雙向鏈表的定義與使用方法,需要的朋友可以參考下2017-10-10
JS實(shí)現(xiàn)網(wǎng)頁(yè)端猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)網(wǎng)頁(yè)端猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

