In Taro, Taro.createSelectorQuery()
an SelectorQuery
object is returned, which provides a method to query element information in the page, such as position, size, and so on. exec()
A method is SelectorQuery
a method in an object that performs a query operation and returns the result.
Specifically, you can Taro.createSelectorQuery()
obtain an SelectorQuery
object through the method, then select()
select the specified element node through the method, and then boundingClientRect()
obtain information such as the position and size of the node through the method. Finally, execute the query operation through exec()
the method, and pass the query result as a parameter to the callback function. For example:
Taro.createSelectorQuery()
.select('#only') // 选取 id 为 only 的节点
.boundingClientRect() // 获取该节点的位置、大小信息
.exec((res) => console.log(res)); // 输出查询结果
In the callback function, res
the parameter is an array, and the elements in the array are query results. The specific format is related to the order in which the query methods are called. For example, if the method is called first select()
, and then the method is called boundingClientRect()
, the elements in the query result are the position and size information of the specified node. If multiple query methods are called, the elements in the query result are an array, and each element in the array corresponds to the result of a query method. It should be noted that the execution of the query operation is asynchronous, so the execution timing of the callback function is also asynchronous.