時間:2023-06-09 22:15:01 | 來源:網(wǎng)站運營
時間:2023-06-09 22:15:01 來源:網(wǎng)站運營
小程序瀑布流組件:支持翻頁與圖片懶加載:電商小程序中,用到瀑布流的地方非常多,每次都寫一個瀑布流,重復(fù)一次邏輯,作為程序員,肯定是非常不愿意的。.├── query-node.js├── waterfall-item.js├── waterfall-item.json├── waterfall-item.wxml├── waterfall-item.wxss├── waterfall.js├── waterfall.json├── waterfall.wxml└── waterfall.wxss
第二步:分別在waterfall.js 和 waterfall-item.js的relations選項中指定組件父、子級關(guān)系:// waterfall.jsComponent({ // ... other code relations: { './waterfall-item': { type: 'child', }, // ... other code }})// waterfall-item.jsComponent({ // ... other code relations: { '././waterfall': { type: 'parent', }, // ... other code }})
指定彼此的父、子組件的關(guān)系后,即可通過 this.getRelationNodes 原生 API,就能訪問彼此實例對象及其屬性和方法。<view class="waterfall custom-class"> <view class="waterfall-inner"> <slot ></slot> </view></view>
同樣,waterfall-item.wxml代碼實現(xiàn)也非常簡單,只有5行代碼:<view class="waterfall-item custom-class" style="{{position}}:0;top:{{(top >= 0 ? top + 'px' : 0 + 'rpx')}};"> <slot ></slot></view>不知道slot用法的童鞋,請參考微信小程序自定義組件模板和樣式文檔。
// query-node.js/** * 獲取當(dāng)前頁面中,選擇器為 selector 的第一個node節(jié)點 * @param {String} selector 符合微信小程序規(guī)范的選擇器 * @param {Object} context 調(diào)用環(huán)境,普通頁面中為wx,自定義組件中為this;默認(rèn)值為wx. * @return {Array} 返回一個數(shù)組,第一個元素為 node 節(jié)點 */export const querySelector = function (selector, context = wx) { return new Promise((resolve, reject) => { context.createSelectorQuery() .select(selector) .boundingClientRect((res) => { if (res) { resolve(res); } else { reject(`不存在選擇器為 ${selector} 的節(jié)點`); } }) .exec(); })};
接著,看一下組件waterfall 和waterfall-item在實際項目中的用法:<waterfall loading="{{loadMorePending}}" isAllLoaded="{{isAllLoaded}}" > <block wx:for="{{data.sections}}" wx:key="id" wx:for-item="product"> <waterfall-item index="{{index}}" custom-class="flow-item-wrapper" > <view class="product-item"> 業(yè)務(wù)代碼 </view> </waterfall-item> </block> </waterfall>
當(dāng)?shù)谝粋€waterfall-item組件,在視圖層布局完成后會執(zhí)行ready生命周期鉤子。// waterfall-item.jsimport { querySelector } from './query-node';Component({ // ... other code lifetimes: { ready() { const [waterfall] = this.getRelationNodes('./waterfall'); this.parent = waterfall; this.setWaterfallItemPosition(); }, } methods:{ async setWaterfallItemPosition() { querySelector('.waterfall-item', this) .then(async (node) => { const { top, position } = await this.parent.getWaterfallItemPostionInfo(node); this.setData({ top, position }) }) }, } // ... other code})
在setWaterfallItemPosition方法中,我們調(diào)用了父組件上的方法// waterfall.jsconst POSITION_LEFT = 'left';const POSITION_RIGHT = 'right';Component({ // ... other code /** * 組件的方法列表 */ methods: { lifetimes: { ready() { this.initParams(); } }, initParams() { this.leftHeights = 0; this.rightHeights = 0; }, /** * 設(shè)置 waterfall-item 的高度值 * @param {Object} node waterfall-item 組件位置尺寸數(shù)據(jù) */ async getWaterfallItemPostionInfo(node) { let top = 0; let position = POSITION_LEFT; const { height } = node; const { itemGap } = this; if (this.leftHeights <= this.rightHeights) { top = this.leftHeights; if(this.leftHeights === 0) { this.leftHeights += height; } else { top += itemGap; this.leftHeights += (height + itemGap); } } else { position = POSITION_RIGHT; top = this.rightHeights; if(this.rightHeights === 0) { this.rightHeights += height; } else { top += itemGap; this.rightHeights += (height + itemGap); } } return { top, position, } } // ... other code }})
當(dāng)所有的waterfall-item重排結(jié)束后,瀑布流渲染完成。<view class="waterfall-item custom-class" style="{{position}}:0;top:{{(top >= 0 ? top + 'px' : itemCount * 100 + 'vh')}};"> <slot ></slot></view>Component({ // waterfall-item.js // ... other code lifetimes: { ready() { const { itemCount } = this.data; const [waterfall] = this.getRelationNodes('./waterfall'); waterfall.childCount += 1; this.parent = waterfall; this.setData({ itemCount: itemCount + waterfall.childCount, }) }, }, // ... other code})
itemGap = waterfall寬度 - (waterfall-item寬度 * 2)
在<waterfall> 的ready鉤子中,可以獲取到<waterfall>組件的寬度;同理,在<waterfall-item>的ready鉤子中,可以獲取到<waterfall-item>組件的寬度。// this.leftHeights += height + itemGap;// or// this.rightHeights += height + itemGap;
具體代碼實現(xiàn)請查看源碼關(guān)鍵詞:圖片,支持,瀑布,程序
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。