美文网首页
RN如何获取当前元素所在位置?

RN如何获取当前元素所在位置?

作者: 风起长林时 | 来源:发表于2018-12-29 13:07 被阅读0次

react native 如何获取当前组件所在位置?

分为两步:

一、在组件上加上ref属性

<Text 
    ref='explainIcon'    // 加上这个
    style={styles.explainIcon}
    onPress={()=>{this.onPress()}}
>
    这样子
</Text>

二、获取当前组件所在位置

    // 点击事件
    onPress=()=>{
        let that=this;
        this.refs.explainIcon.measure((frameX, frameY, frameWidth, frameHeight, pageX, pageY)=>{
            console.log(frameX);
            console.log(frameY);
            console.log(frameWidth);    // 当前组件的宽度
            console.log(frameHeight);   // 当前组件的高度
            console.log(pageX);         // 当前组件距离左侧的距离
            console.log(pageY);         // 当前组件距离顶部的距离
        });

    }

相关文章

网友评论

      本文标题:RN如何获取当前元素所在位置?

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