收藏
回答

小程序插件获取元素的宽度?

在插件里面获取元素的宽度报错:

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

回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容
问题标签