小程序插件获取元素的宽度?
在插件里面获取元素的宽度报错:
wxml:
<image class="section1-image" src="{{saasOb.cropImage}}" bindload="imageLoad" id="section1Image" />
js:
imageLoad: function(e) {
//选择id
var that = this;
const query = wx.createSelectorQuery()
query.select('#section1Image').boundingClientRect()
query.selectViewport().scrollOffset()
query.exec(function (res) {
console.log(JSON.stringify(res))
res[0].width
console.log("rect.width:" +res[0].width)
that.setData({
// height: rect.width + 'px'
imageWidth: res[0].width * 2
})
})
},
Consolo:
[null,{“id”:"",“dataset”:{},“scrollLeft”:0,“scrollTop”:0,“scrollWidth”:379,“scrollHeight”:635}]
thirdScriptError
Cannot read property ‘width’ of null;at SelectorQuery callback function
TypeError: Cannot read property ‘width’ of null
1 回复
在自定义组件或包含自定义组件的页面中,应使用 this.createSelectorQuery()
来代替。
看看是不是这个问题
文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/wxml/wx.createSelectorQuery.html