The implementation of getting element width and height in uni app component

Time:2021-1-22

Problems encountered: create a selector directly in the component mounted to obtain the width and height of the element. Even if it is thrown into the timer to execute, there are still occasional cases where it cannot be obtained

terms of settlement:

//Writing method 1:
getDescBox() { 
  uni.createSelectorQuery().in(this).select('.top .desc').boundingClientRect(result => { 
   if (result) { 
     console.log('==========',result) 
   }else { 
     this.getDescBox(); 
 } 
  }).exec(); 
},

//Writing method 2:
getDescBox() { 
  uni.createSelectorQuery().in(this).select('.top .desc').boundingClientRect().exec((res)=>{ 
   if (res\[0\]) { 
     this.descHeight \= res\[0\].height; 
 }else { 
     this.$nextTick(()=>{ 
      this.getDescBox(); 
 }) 
   } 
  }) 
},

The two is as like as two peas.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.