美文网首页
Taro 获取组件离顶部高度

Taro 获取组件离顶部高度

作者: blossom_6694 | 来源:发表于2020-03-31 17:54 被阅读0次
    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)
    

    相关文章

      网友评论

          本文标题:Taro 获取组件离顶部高度

          本文链接:https://www.haomeiwen.com/subject/eelguhtx.html