javascript-React Native:获取元素的位置
我正在将带有flexbox的Image组件设置为位于屏幕中央,效果很好。 现在,我希望将第二个.offset()组件直接显示在第一个组件的顶部。 第二个图像使用绝对定位。 目前,我只是在猜测像素是否适合它,但是当然这是不准确的,并且在可维护性方面付出了太多努力。
我正在寻找与jQuery .offset()等效的React Native。是否有这样的事情,如果没有,实现这一目标的最佳方法是什么?
5个解决方案
73 votes
React Native提供了setTimeout( myComponent.measure(...), 0 )方法,该方法接受回调,并使用组件的偏移量和宽度/高度对其进行调用:
myComponent.measure( (fx, fy, width, height, px, py) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
console.log('X offset to frame: ' + fx)
console.log('Y offset to frame: ' + fy)
console.log('X offset to page: ' + px)
console.log('Y offset to page: ' + py)
})
例...
以下内容将呈现