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

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)
原文地址:https://www.cnblogs.com/jane2160/p/11307217.html