微信小程序获取某个元素的高度宽度

1,521 阅读1分钟

获取小程序的元素高度和宽度,得在js页面使用this下的height: rect.width + 'px'

看下代码

js页面
//创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    query.select('.every').boundingClientRect(function (rect) {
      // console.log(rect.width)
      that.setData({
        height: rect.width + 'px'
      })
    }).exec();
wxml页面

<view class='every' style='height:{{height}}'></view>