CNCEC_APP/uni_modules/uview-pro/libs/hooks/useRect.ts

41 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getCurrentInstance, nextTick, ref } from 'vue';
/**
* useRect - 获取元素的位置信息(响应式,原生实现)
* @param selector 选择器(如 #id 或 .class
* @param all 是否获取所有匹配元素
* @returns rect 响应式的节点信息refresh 主动刷新方法
*/
export function useRect(selector: string | null = null, all = false) {
const rect = ref<any>(all ? [] : null);
const instance = getCurrentInstance();
async function getRect(realSelector: string | null = null, delay = 0): Promise<any> {
realSelector = realSelector || selector;
if (!realSelector) return rect.value;
await nextTick();
return new Promise(resolve => {
setTimeout(() => {
uni.createSelectorQuery()
.in(instance?.proxy)
[all ? 'selectAll' : 'select'](realSelector as string)
.boundingClientRect((res: any) => {
rect.value = res;
resolve(res);
})
.exec();
}, delay);
});
}
function refresh(selector?: string | null, delay?: number): Promise<any> {
return getRect(selector, delay);
}
return {
rect,
getRect,
refresh
};
}