小程序如何動態監聽頁面的高度

沒有找到好的方法或者官方的相關API
1)嘗試了網上說的這個,但是onready只調用一次,所以這個寫了定時器也還是執行一次:

setTimeout(() => {
      let _this = this
      wx.createSelectorQuery().select('#container-title').boundingClientRect(function (rect) {
      width = rect.width
      height = rect.height
      top = rect.top
    }).exec()
    },300)

2)還是把這個獲取頁面高度的方法寫成的公共的方法,然後進行改變頁面高度的操作(比如新增刪除、顯示隱藏)的時候就調用一下這個方法

onReady: function () {
      //動態獲取高度
    this.autoHeight();
  },
  //動態獲取高度
  autoHeight:function(){
      var that = this;
      var query = wx.createSelectorQuery();
      query.select('.container').boundingClientRect(function(rect) {
        console.log("----w" + rect.height)
        that.setData({
          windowHeight: rect.height ,//獲取頁面高度
        })
      }).exec();
  },

還是有點麻煩其實,後續找到更好的解決辦法再進行更新……
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章