这是因为uni.createSelectorQuery().boundingClientRect()是异步获取元素节点高度,当请求的数据过多或者过长时无法获取准确的高度
目前我解决的方法是加了个超时定时器
onReady() {
let div = document.querySelector(".center-class");
let that = this
uni.getSystemInfo({
success(res) {
setTimeout(() => {
let screenHeight = res.screenHeight
uni.createSelectorQuery().select('.top-class').boundingClientRect(data => {
that.topHeight = data.height
console.log(data)
}).exec()
uni.createSelectorQuery().select('.bottom-class').boundingClientRect(data => {
let bottomHeight = data.height
that.centerHeight =
`calc(100% - ${that.topHeight}px - ${bottomHeight}px)`
console.log(that.topHeight, bottomHeight)
that.$nextTick(() => {
div.style.height = that.centerHeight
div.style.top = (that.topHeight - 10) + "px"
})
}).exec()
}, 10)
}
})
},