记录小程序获取元素的高度或宽度

wx.createSelectorQuery().selectAll('.npl-intro').boundingClientRect(function (rect) {
    console.log(rect[0].height)
    console.log(rect[0].width)
}).exec()  

  如果觉得这样写有点长。可以分步写。也是一样的结果。

复制代码
var obj=wx.createSelectorQuery();
obj.selectAll('.npl-intro').boundingClientRect(function (rect) {
    console.log(rect[0].height)
    console.log(rect[0].width)
})
obj.exec() ;
复制代码

 或者在exec中返回,如果出现上面的方式出现获取到的 rect 是 null 的话,可以考虑用下面这种,就不会出现问题。结果是一样的。

复制代码
var obj=wx.createSelectorQuery();
obj.selectAll('.npl-intro').boundingClientRect();
obj.exec(function (rect) {
    console.log(rect[0].height)
    console.log(rect[0].width)
}) ;
posted @ 2020-01-09 14:38  法国梧桐和小伙子  阅读(1898)  评论(0)    收藏  举报