Taro 获取组件离顶部高度
2020-03-31 本文已影响0人
blossom_6694
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)