utils.js 548 B

12345678910111213141516
  1. // 获取元素宽高
  2. export const getRect = ({ name, onSuccess, instance }) => {
  3. // 创建一个选择器查询对象
  4. const select = uni.createSelectorQuery().in(instance.proxy);
  5. // 选择指定元素,并获取其矩形信息
  6. select
  7. .select(name)
  8. .boundingClientRect((rect) => {
  9. // 如果rect是一个数组,则返回
  10. if (!rect) return;
  11. if (Array.isArray(rect)) return rect[0];
  12. // 如果onSuccess存在,则调用onSuccess函数,并传入rect的高度
  13. onSuccess?.(rect);
  14. })
  15. .exec();
  16. };