获取小程序获取顶部栏高度

914 阅读1分钟

小程序可以通过 wx.getMenuButtonBoundingClientRect() 获取胶囊按钮的信息 和 wx.getSystemInfo() 获取设备信息。

给wx.getMenuButtonBoundingClientRect()方法定义一个变量(menuinformation)

let menuinformation=wx.getMenuButtonBoundingClientRect()
  

获取到胶囊按钮信息:

高:height,---距顶部的高:top

wx.getSystemInfo()获取设备信息,

状态栏高度:statusBarHeight

整个顶部的高度为(navHeight)

navHeight = (menuinformation.top-statusBarHeight)*2 + statusBarHeight + menuinformation.height

整个顶部的高 = (距顶部的高-状态栏高)*2 + 状态栏高 + 胶囊自身高