UNPKG

@z-cloud/virtual-taro

Version:

一个基于React + TS开发的虚拟列表,支持瀑布流、grid的组件。

62 lines (61 loc) 1.65 kB
import Taro from "@tarojs/taro"; const virtualizerUUID = { value: 1 }; function getRectSize(id, success, fail, retryMs = 500) { const query = Taro.createSelectorQuery(); try { query.select(`#${id}`).boundingClientRect((res) => { if (res instanceof Array ? res.length > 0 : res) { success == null ? void 0 : success(res); } else { fail == null ? void 0 : fail(); } }).exec(); } catch (err) { setTimeout(() => { getRectSize(id, success, fail, retryMs); }, retryMs); } } function getRectSizeAsync(id, retryMs = 500, retryTimes = 3) { return new Promise((resolve) => { function retry() { if (retryTimes <= 0) return; setTimeout(async () => { try { const res = await getRectSizeAsync(id, retryMs, --retryTimes); resolve(res); } catch (err) { retry(); } }, retryMs); } getRectSize(id, resolve, retry, retryMs); }); } async function getScrollViewContextNode(id) { const query = Taro.createSelectorQuery(); return new Promise( (resolve) => query.select(`#${id}`).node((res) => resolve(res == null ? void 0 : res.node)).exec() ); } function getWindowRect() { try { const info = Taro.getWindowInfo(); if (!info.windowWidth) { throw "getWindowInfo error"; } return { width: info.windowWidth, height: info.windowHeight }; } catch (error) { const legcay = Taro.getSystemInfoSync(); return { width: legcay.windowWidth, height: legcay.windowHeight }; } } export { getRectSize, getRectSizeAsync, getScrollViewContextNode, getWindowRect, virtualizerUUID };