收藏
回答

自定义顶部有个别时候wx.getMenuButtonBoundingClientRe赋值不成功问题?

极个别情况,赋值不上,已经出现了两次了,但是没有找到可以复现的方法

1、app.js页面主要代码

onLaunch: function () {
    // 获取设备信息
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        // 获取右上角胶囊的位置信息
        let info = wx.getMenuButtonBoundingClientRect()  // { bottom: 58, height: 32, left: 278, right: 365, top: 26, width: 87 },单位为 px
        let CustomBar = info.bottom + info.top - res.statusBarHeight;
        _this.globalData.customBarHeigh = CustomBar;
        _this.globalData.customBarTop = info.top;     
      }
    })
},
globalData: {
    customBarHeigh: '', //自定义导航条的高度
    customBarTop: '',
}

2、使用的页面主要代码:

js 主要代码:
onLoad: function (options) {
    wx.hideShareMenu();
    this.getSetInfoData();
  },


  getSetInfoData(){
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    query.select('.preMonthlyTop').boundingClientRect(function (rect) {
      that.setData({
        preMonthlyTopH: rect.height
      })
    }).exec();

    this.setData({
      customBarHeigh: app.globalData.customBarHeigh,
      customBarTop: app.globalData.customBarTop,
      returnTop: app.globalData.customBarTop * 2 + (64 - 52) / 2
    })
    this.getOrderCount();
  }

wxml主要代码:
<view class='contentBox' style="margin-top: {{customBarHeigh+7}}px;">
      <view class="preMonthlyTop">
        ......
      </view>
</view>

注:极个别情况customBarHeigh,赋值不上,已经出现了两次了,但是没有找到可以复现的方法,求解决问题
回答关注问题邀请回答
收藏

2 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2020-04-23
    应该存在同步异步问题
    给一个默认值,或者,在app.js中写一个方法
    
    getCustomBarInfo: function() {
      var t = this
      return new Promise((rs, rj) => {
        if (t.globalData.customBarInfo && t.globalData.customBarInfo.customBarHeigh){
          rs(t.globalData.customBarInfo)
        } else {
          wx.getSystemInfo({
            success: function (res) {
              // 获取右上角胶囊的位置信息
              let info = wx.getMenuButtonBoundingClientRect()  // { bottom: 58, height: 32, left: 278, right: 365, top: 26, width: 87 },单位为 px
              let CustomBar = info.bottom + info.top - res.statusBarHeight;
              t.globalData.customBarInfo = { customBarHeigh: CustomBar, customBarTop = info.top}
              rs(t.globalData.customBarInfo)
            },
            fail:(e)=>{ rj(e) }
          })
        }
        
      })
    }
    =================
    用到的地方在调用
    app.getCustomBarInfo().then(res=>{
      this.setData({
        customBarHeigh: res.customBarHeigh,
        customBarTop: res.customBarTop
      })
    })
    

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

    2020-04-23
    有用 1
    回复 5
  • 睡前原谅一切
    睡前原谅一切
    2020-04-23
    看下后台报错。是不是有wx.getMenuButtonBoundingClientRect()不是一个function的报错。
    
    2020-04-23
    有用
    回复 9
    • 🌻Sunny
      🌻Sunny
      2020-04-23
      当时那样的时候,没有看后台的报错,如果是报错不是一个function ,有解决方法吗?
      2020-04-23
      回复
    • 睡前原谅一切
      睡前原谅一切
      2020-04-23回复🌻Sunny
      调高基础库。有的用户微信版本过低。基础库里没有这个api
      2020-04-23
      回复
    • 🌻Sunny
      🌻Sunny
      2020-04-23
      现在的基础库是2.10.3了呢,出现过这个问题的微信版本7.0.12
      2020-04-23
      回复
    • 🌻Sunny
      🌻Sunny
      2020-04-23
      你们的基础库一般是多少呀?
      2020-04-23
      回复
    • 睡前原谅一切
      睡前原谅一切
      2020-04-23回复🌻Sunny
      那应该没问题 不是这个问题。7.0.12的基础库有这个api
      2020-04-23
      回复
    查看更多(4)
登录 后发表内容
问题标签