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)