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