微信小程序提供了一個獲取節點寬高等信息的方法,一共是兩步。
1.在對應的節點上定義一個id
<image src='{{imgUrl}}' class="look-image" bindload="imageLoad" id="drawID"></image>
2.在js里面創建這個id的節點選擇器
onLoad: function (options) { var _this=this; //創建節點選擇器 var query = wx.createSelectorQuery(); //選擇id query.select('#drawID').boundingClientRect() query.exec(function (res) { //res就是 該元素的信息 數組 console.log(res); //取高度 _this.setData({ realWidth: res[0].width, realHeight: res[0].height }) console.log('取高度', _this.data.realHeight); }) }