小程序 wx.getSystemInfo 获取屏幕的windo

2021-06-07  本文已影响0人  冰落寞成

wx.getSystemInfo

为同步获取系统信息,有时候拿到的数据并不准确

解决方案

在onReady() 里使用getSystemInfo 异步获取

onReady(){
    wx.getSystemInfo({
      success:res=>{
        const width = res.windowWidth;
        const height = res.windowHeight;
        const screenHeight = res.screenHeight
        const screenWidth = res.screenWidth
        const statusBarHeight = res.statusBarHeight
        let radio = Math.floor(res.windowWidth / 750 * 100) / 100
        app.globalData.height = (height-statusBarHeight)*(1/radio)
        app.globalData.screenHeight = (screenHeight-statusBarHeight)*(1/radio)
        app.globalData.width = width*(1/radio)
        app.globalData.radio = radio
        app.globalData.readyScreenHeight = screenHeight
        app.globalData.readyScreenWidth = screenWidth
        app.globalData.readyHeight = height
        app.globalData.readyWidth = width
        app.globalData.pixelRatio = res.pixelRatio
        // console.log('ready',width,height,res.screenHeight,res.statusBarHeight,radio,app.globalData.height,app.globalData.width)
        setTimeout(()=>{
          this.getHeaderHeight()
        },200)
      }
    })
   
  },
上一篇 下一篇

猜你喜欢

热点阅读