微信小程序 获取组件高度Height

调试基础库:2.9.3

js中

    /* 获取当前绑定id的高度 */
    getIdHeight(e){
      var id;
      /*
        获取组件id值,这个id需要自己设置,也可以用class、data-operation等等代替,只要能索引到        
        这个组件就行
      */
      if (e.currentTarget.id) {
        id = "#" + e.currentTarget.id;
      }
      else {
        return false;
      }
        //非自定义组件请改成 wx.createSelectorQuery();
      const query = this.createSelectorQuery(); 
       /* 
        只要这里的id换成id或是class都行,比如有个class="btn",这里就可以写为
        query.select(".btn").boundingClientRect(); 
        比如有个id="btn",这里就可以写为query.select("#btn").boundingClientRect(); 
        */
      query.select(id).boundingClientRect(); 
      query.selectViewport().scrollOffset();
      query.exec((res) => {
        //可换成width,其他属性在res中,可以console.log(res)查看
        var idHeight = res[0].height; 
        if(idHeight) return idHeight;
        else return false;
    });
    }

wxml中绑定

即可点击button返回其高度值。

你可能感兴趣的:(微信小程序)