1.页面中使用

uni.createSelectorQuery().select(`#${id}`).boundingClientRect(res => {
    console.log('=====res====', res)     
}).exec()

2.组件中使用增加 in(this) 或使用 this.createSel.....

uni.createSelectorQuery().in(this).select(`#${id}`).boundingClientRect(res => {
    console.log('=====res====', res)     
}).exec()
this.createSelectorQuery().select(`#${id}`).boundingClientRect(res => {
    console.log('=====res====', res)     
}).exec()

 3.其他写法

uni.createSelectorQuery().select(`#${id}`).boundingClientRect().exec(res => {
  console.log('======res======', res[0])
})

4.boundingClientRect返回值为null

   1.确保页面已经渲染完成再计算高度,即已存在该id标识的元素,常见解决办法是 增加定时器延迟100ms,或在onLoad中去调用。

   2.对view动态设置id时,确保id首位是字母,千万不能是数字,否则wx小程序无法识别。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐