export function queyElementYPosition(selector: string, component?: any): Promise<clientRectElement> {
let query: SelectorQuery
if (process.env.TARO_ENV == 'h5') {
query = Taro.createSelectorQuery().in(component)
} else {
query = Taro.createSelectorQuery().in(component.$scope)
}
return new Promise((resolve, reject) => {
const tmp = query.select(selector)
.fields({ size: true, rect: true, scrollOffset: true, dataset: true }, (rect: fieldElement) => {
!!rect ? resolve(rect) : reject(rect);
});
tmp && tmp.exec();
})
}
使用:
setTimeout(() => {
queyElementYPosition('#filter-placeholder', this)
.then((v) => controller.setYPosition(v.top))
.catch((e) => console.error('query element y position failed.', e))
}, 300)
网友评论