1.若是高度要px单位的话:异步
let query = wx.createSelectorQuery(); query.select('.content').boundingClientRect(rect=>{ let height = rect.height; console.log(height); }).exec();
2.若是高度要rpx单位的话,那么能够用宽高比换算得到:(如下的750是该元素的宽度,单位是rpx的)code
let query = wx.createSelectorQuery(); query.select('.content').boundingClientRect(rect=>{ let clientHeight = rect.height; let clientWidth = rect.width; let ratio = 750 / clientWidth; let height = clientHeight * ratio; console.log(height); }).exec();
3.在页面渲染完成OnReady回调 获取元素高度时,若是不加定时器,获取的元素的高度仍是没渲染完异步数据前的高度。故须要加定时器io
onReady () { setTimeout(() => { let query = wx.createSelectorQuery(); query.select('.content').boundingClientRect(rect=>{ let height = rect.height; console.log(height); }).exec(); }, 300) }