获取组件内某个view的高度始终为0?
发布于 4 年前 作者 myi 4733 次浏览 来自 问答

创建了一个自定义组件

<view class="modals-bottom-dialog">
  <view id="dialogBody" class="bottom-dialog-body"></view>
</view>

.modals-bottom-dialog {
  position:fixed; 
  z-index: 999; 
  top:0; 
  left: 0; 
  right:0; 
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
}
.bottom-dialog-body {
  position:absolute; 
  z-index:1000; 
  bottom:0; 
  left:0; 
  right:0; 
  height: 400rpx;
  background-color: aquamarine;
}

此时想要获取id=dialogBody的组件的宽高,在ready中执行返回为0,0

ready() {
    const obj = this.createSelectorQuery()
    obj.select('#dialogBody').boundingClientRect()
    obj.exec(function(rect) {
      contentWidth = rect[0].width
      contentHeight = rect[0].height
    });
},

rect的每个数值都为0?为什么?加了settimeout延时获取,也一样是0

 

1 回复

```

wx.createSelectorQuery()

            .select("#innerRef")

            .boundingClientRect(function (inner) {

console.log(inner)

            })

            .exec();

```

回到顶部