2022-09-08 03:27:26 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 获取元素节点信息(在组件中的元素必须要传ctx)
|
|
|
|
|
|
* @param { String } selector 选择器 '.app' | '#app'
|
|
|
|
|
|
* @param { Boolean } all 是否多选
|
|
|
|
|
|
* @param { ctx } context 当前组件实例
|
|
|
|
|
|
*/
|
2022-09-09 01:50:39 +00:00
|
|
|
|
export const getRect = (selector: string, all = false, context?: any) => {
|
2022-09-08 03:27:26 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
let qurey = uni.createSelectorQuery()
|
|
|
|
|
|
if (context) {
|
|
|
|
|
|
qurey = uni.createSelectorQuery().in(context)
|
|
|
|
|
|
}
|
2022-09-09 01:50:39 +00:00
|
|
|
|
qurey[all ? 'selectAll' : 'select'](selector)
|
|
|
|
|
|
.boundingClientRect(function (rect) {
|
|
|
|
|
|
if (all && Array.isArray(rect) && rect.length) {
|
|
|
|
|
|
return resolve(rect)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!all && rect) {
|
|
|
|
|
|
return resolve(rect)
|
|
|
|
|
|
}
|
|
|
|
|
|
reject('找不到元素')
|
|
|
|
|
|
})
|
|
|
|
|
|
.exec()
|
2022-09-08 03:27:26 +00:00
|
|
|
|
})
|
2022-09-09 01:50:39 +00:00
|
|
|
|
}
|