uniapp之boundingClientRect注意事项

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小程序无法识别。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/060970511a.html