在当前页面下选择第一个匹配选择器selector的节点,返回一个NodesRef对象实例,可以用于获取节点信息。
selector类似于CSS的选择器,但仅支持下列语法。
#the-id.a-class.another-class.the-parent > .the-child.the-ancestor .the-descendant.the-ancestor >>> .the-descendant#a-node, .some-other-nodes

在当前页面下选择匹配选择器selector的节点,返回一个NodesRef对象实例。 与selectorQuery.selectNode(selector)不同的是,它选择所有匹配选择器的节点。
选择显示区域,可用于获取显示区域的尺寸、滚动位置等信息,返回一个NodesRef对象实例。
添加节点的布局位置的查询请求,相对于显示区域,以像素为单位。其功能类似于DOM的getBoundingClientRect。返回值是nodesRef对应的selectorQuery。
返回的节点信息中,每个节点的位置用left、right、top、bottom、width、height字段描述。如果提供了callback回调函数,在执行selectQuery的exec方法后,节点信息会在callback中返回。
示例代码:
Page({
getRect: function(){
wx.createSelectorQuery().select('#the-id').boundingClientRect(function(rect){
rect.id // 节点的ID
rect.dataset // 节点的dataset
rect.left // 节点的左边界坐标
rect.right // 节点的右边界坐标
rect.top // 节点的上边界坐标
rect.bottom // 节点的下边界坐标
rect.width // 节点的宽度
rect.height // 节点的高度
}).exec()
},
getAllRects: function(){
wx.createSelectorQuery().selectAll('.a-class').boundingClientRect(function(rects){
rects.forEach(function(rect){
rect.id // 节点的ID
rect.dataset // 节点的dataset
rect.left // 节点的左边界坐标
rect.right // 节点的右边界坐标
rect.top // 节点的上边界坐标
rect.bottom // 节点的下边界坐标
rect.width // 节点的宽度
rect.height // 节点的高度
})
}).exec()
}
})
更多微信小程序开发教程,关注多程序。
