关于小程序获取view的动态高度填坑

时间:2019-08-06 10:43:58   收藏:0   阅读:339
wx.createSelectorQuery().select(‘#box‘).boundingClientRect(function (rect) {
      width = rect.width
      height = rect.height
      top = rect.top
    }).exec()

如上,拿到了id为box的view,并获取到了它的宽、高等属性,此段代码要放在onReady函数中

注意:如果这个view的宽高是随着内容而变化的话,这样获取到的宽高就有可能还是渲染完成前的值,不知是不是小程序自己的bug,解决方案是在获取外加一个定时器,如下

setTimeout(() => {
      let _this = this
      wx.createSelectorQuery().select(‘#container-title‘).boundingClientRect(function (rect) {
      width = rect.width
      height = rect.height
      top = rect.top
    }).exec()
    },300)

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!