小程序實現(xiàn)瀑布流動態(tài)加載列表
本文實例為大家分享了小程序實現(xiàn)瀑布流動態(tài)加載列表的具體代碼,供大家參考,具體內容如下
最近業(yè)務需要做一個商城列表,就自己寫了一個瀑布流來加載列表。

這個列表在很多地方都需要用到,就給寫成組件,方便使用。
1、goodsBox.js代碼
想法很簡單,就是判斷兩列的高度,將數(shù)據(jù)插入低的一列。
let leftHeight = 0,
? rightHeight = 0; //分別定義左右兩邊的高度
let query;
Component({
? /**
? ?* 組件的屬性列表
? ?*/
? properties: {
? ? goodsList: {
? ? ? type: Array,
? ? ? value: []
? ? },
? ? loadingShow: {
? ? ? type: Boolean,
? ? ? value: false
? ? },
? ? noInfoShow: {
? ? ? type: Boolean,
? ? ? value: false
? ? }
? },
? /**
? ?* 組件的初始數(shù)據(jù)
? ?*/
? data: {
? ? leftList: [],
? ? rightList: []
? },
? observers: {
? // 當goodsList發(fā)生變化時調用方法
? ? 'goodsList': function (goodsList) {
? ? ? this.isLeft()
? ? }
? },
? /**
? ?* 組件的方法列表
? ?*/
? methods: {
? ? async isLeft() {
? ? ? const {
? ? ? ? goodsList,
? ? ? ? leftList,
? ? ? ? rightList
? ? ? } = this.data;
? ? ? query = wx.createSelectorQuery().in(this);
? ? ? let list = goodsList.slice(leftList.length+rightList.length,goodsList.length)
? ? ? for (const item of list) {
? ? ? ? leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判斷兩邊高度,來覺得添加到那邊
? ? ? ? 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();
? ? ? ? ? });
? ? ? ? });
? ? ? })
? ? },
? }
})這一塊需要注意的是 wx.createSelectorQuery().in(this),將選擇器的選取范圍更改為自定義組件 component 內。微信文檔.
2、goodsBox.wxml
這邊主要就是把頁面分成兩列,一些css就不多寫了
// wxml
<view class="box clearfix">
? <view id="left" class="left">
? ? <view class="shop_box" wx:for="{{leftList}}" wx:key="index" ></view>
? </view>
? <view id="right" class="right">
? ? <view class="shop_box" wx:for="{{rightList}}" wx:key="index" ></view>
? </view>
</view>
<view class="cu-load ?loading" wx:if="{{loadingShow}}"></view>
<view class="cu-load ?over" wx:if="{{noInfoShow}}"></view>3、goodsBox.wxss
.left,.right{
? float: left;
}
.clearfix::after {
? content: ".";
? clear: both;
? display: block;
? overflow: hidden;
? font-size: 0;
? height: 0;
}
.clearfix {
? zoom: 1;
}4、頁面使用
現(xiàn)在json文件里面引用組件,然后使用組件
<goodsBox id="goodsBox" goodsList="{{goodsList}}" loadingShow="{{loadingShow}}" noInfoShow="{{noInfoShow}}"></goodsBox>每次goodsList發(fā)生變化,組件就會調用瀑布流排列方法。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IE中document.createElement的iframe無法設置屬性name的解決方法
這篇文章主要介紹了IE中document.createElement的iframe無法設置屬性name的解決方法,需要的朋友可以參考下2015-09-09
layer.open回調獲取彈出層參數(shù)的實現(xiàn)方法
今天小編就為大家分享一篇layer.open回調獲取彈出層參數(shù)的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
微信小程序實現(xiàn)限制用戶轉發(fā)功能的實例代碼
這篇文章主要介紹了微信小程序實現(xiàn)限制用戶轉發(fā)的實例代碼,通過截圖加實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
使用window.prompt()實現(xiàn)彈出用戶輸入的對話框
window對象的最后一種對話框是提示對話框,它顯示了預先設置的信息并提供文本域供用戶輸入應答。它包括兩個按鈕,即Cancel和Ok,允許用戶用兩個相反的期望值來關閉這個對話框:取消整個操作或接收輸入到對話框中的文本2015-04-04

