前言
瀑布流 - 小程序中數(shù)據(jù)列表或者圖片展示難免高度不一,而我們一般固定寬度耙蔑,讓其高度自適應(yīng)泄朴,并無(wú)縫對(duì)接蔬顾。如下圖:
瀑布流的兩種做法:
- css:
在父元素上使用column-count: 2也可以做到兩列排版。但column-count: 2是左右兩列排版昭齐,如果碰巧出現(xiàn)左邊累計(jì)高度過(guò)多大于右邊累計(jì)高度的話尿招,右邊會(huì)出現(xiàn)大片空白區(qū)域。點(diǎn)擊前往 - js(推薦):
判斷左右兩邊的累計(jì)高度阱驾,那邊的高度小就谜,便往那邊添加(需要?jiǎng)討B(tài)獲取節(jié)點(diǎn)元素的高度進(jìn)行計(jì)算)。下面用的就是此種方法:
準(zhǔn)備工作
我們需要用到 await 里覆,而使用 await 需要在本地配置中開(kāi)啟增強(qiáng)編譯丧荐。如下圖:
實(shí)現(xiàn)代碼
此方法采用左右兩列布局,判斷兩邊高度喧枷。如果左<右篮奄,填充到左邊,否則填充到右邊
(list不建議一次請(qǐng)求過(guò)多數(shù)據(jù)割去,過(guò)多的數(shù)據(jù)可能會(huì)造成加載慢窟却,可用圖片懶加載優(yōu)化)
- test.wxml
<!--pages/test/test.wxml-->
<view class="container">
<view id="left">
<view wx:for="{{leftList}}" wx:key>
<image src="{{item.url}}" mode='widthFix'></image>
<text>{{item.title}}</text>
</view>
</view>
<view id="right">
<view wx:for="{{rightList}}" wx:key>
<image src="{{item.url}}" mode='widthFix'></image>
<text>{{item.title}}</text>
</view>
</view>
</view>
- test.wxss
/* pages/test/test.wxss */
#left, #right{
width: 48%;
margin: 0 1%;
float: left;
}
#left image, #right image{
width: 100%;
}
- test.js
//test.js
let leftHeight = 0, rightHeight = 0; //分別定義左右兩邊的高度
let query;
Page({
data: {
list: [{
title: '1',
url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
}, {
title: '2',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
},{
title: '3',
url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
},{
title: '4',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
},{
title: '5',
url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_9E39DA252E3946BE36218D85876C4AB4.jpg',
},{
title: '6',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
},{
title: '7',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
},{
title: '8',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
},{
title: '9',
url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72'
},{
title: '7',
url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg'
},{
title: '8',
url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg'
},{
title: '9',
url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
},{
title: '10',
url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
}],
leftList: [],
rightList: []
},
onLoad() {
this.isLeft();
},
async isLeft() {
const { list, leftList, rightList } = this.data;
query = wx.createSelectorQuery();
for (const item of list) {
leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判斷兩邊高度,來(lái)覺(jué)得添加到那邊
await this.getBoxHeight(leftList, rightList);
}
},
getBoxHeight(leftList, rightList) { //獲取左右兩邊高度
return new Promise((resolve, reject) => {
this.setData({ leftList, rightList }, () => {
query.select('#left').boundingClientRect();
query.select('#right').boundingClientRect();
query.exec((res) => {
leftHeight = res[0].height; //獲取左邊列表的高度
rightHeight = res[1].height; //獲取右邊列表的高度
resolve();
});
});
})
}
})