Get Element Height in Uniapp: Complete Guide

In uniapp, you can use the uni.createSelectorQuery() method to obtain the height of an element.

The code sample is as follows:

// 获取元素高度
uni.createSelectorQuery().select('.element-class').boundingClientRect(function(rect) {
  console.log('元素高度:', rect.height);
}).exec();

.element-class is the class name of the element whose height needs to be retrieved, and can be replaced with the corresponding selector as needed.

You can use the boundingClientRect method to access the height of an element (rect.height) and perform further operations as needed.

It is important to note that the method createSelectorQuery() returns a selector object, specifying the selector of an element through the select() method, then obtaining the height of the element through the boundingClientRect() method. Lastly, executing the selector query through the exec() method.

Furthermore, the uni.createSelectorQuery() method is exclusive to uniapp and can only be used within uniapp, not compatible with other front-end frameworks or pure native development.

bannerAds