小程序官方组件navigation-bar高度变动问题

写了一个《小鸟听写》小程序,调试的时候导航栏是正常的,提交的时候页首的标题缺上升到被摄象头挡住了。首先出现这个问题的原因,可能是因为博主在用win7,开发工具的版本过低导致组件代码也过于老旧造成的。但是因为博主并不需要实现过于负责的功能,暂不准备学习使用第三方组件,于是看了下官方组件的js文件--navigation-bar.js:

    attached() {
      const rect = wx.getMenuButtonBoundingClientRect()
      const platform = (wx.getDeviceInfo() || wx.getSystemInfoSync()).platform
      const isAndroid = platform === 'android'
      const isDevtools = platform === 'devtools'
      const { windowWidth, safeArea: { top = 0, bottom = 0 } = {} } = wx.getWindowInfo() || wx.getSystemInfoSync()
      this.setData({
        ios: !isAndroid,
        innerPaddingRight: `padding-right: ${windowWidth - rect.left}px`,
        leftWidth: `width: ${windowWidth - rect.left}px`,
        safeAreaTop: isDevtools || isAndroid ? `height: calc(var(--height) + ${top}px); padding-top: ${top}px` : ``
      })
    },

影响这个高度的应该是其中的safeAreaTop参数。利用deepseek做如下修改后,高度就稳定了:

    attached() {
      const rect = wx.getMenuButtonBoundingClientRect();
      const deviceInfo = wx.getDeviceInfo();
      const windowInfo = wx.getWindowInfo();
      const platform = deviceInfo.platform;
      const isAndroid = platform === 'android';
      const windowWidth = windowInfo.windowWidth;
      const statusBarHeight = windowInfo.statusBarHeight || 0;
      const menuButtonHeight = rect.height || 32;
      const navHeight = statusBarHeight + menuButtonHeight + 8;
      const menuButtonRight = windowWidth - rect.left;
      this.setData({
        ios: !isAndroid,
        innerPaddingRight: `padding-right: ${menuButtonRight}px`,
        leftWidth: `width: ${menuButtonRight}px`,
        safeAreaTop: `height: ${navHeight}px; padding-top: ${statusBarHeight}px;`,
      });
    },

有遇到同样的问题的亲,可以留意一下这个函数。

标签: 微信

移动端可扫我直达哦~

推荐阅读

thumbnail 2026-01-16

小程序的navigation-bar组件高度的计算与使用

博主只是想实现一个听写的小功能,所以并不需要过多的拓展,官方导航组件就很好用。就是从网页端换到了小程序端,不太清楚如何去计算这个navigation-bar的高度,好在现在有ai工具,特别deepseek是触手可及的好工具。白天刷到一...

建站相关 微信

thumbnail 2026-01-13

微信小程序开发学习笔记之模板

小程序模板(Template)是一种可复用的WXML代码片段,主要用于展示层(UI)的复用。小程序模板只包含WXML结构,可关联WXSS样式,不包含逻辑(无.js文件),没有独立作用域(样式会继承页面的样式)。一个小实例<!--...

建站相关 微信

thumbnail 2026-01-11

小鸟听写音频服务器的搭建流程

安装ffmpeg服务器用的阿里云,因为pip仓库里没有python的ffmpeg模块,所以退求其次,准备利用静态编译好的版本,上传到服务器之后,利用下面的命令解压缩:tar -xf ffmpeg-n7.1-62-gb168ed9b14...

建站相关 微信

thumbnail 2026-01-05

模拟器空白?win7环境下的微信小程序开发

小鸟数据的服务器到期,更换了阿里云的99计划的服务器,配置是稍微高了一点,但公网ip有点大问题,起初只是发现没办法curl访问一些常用的外部站点,这还只是小事,用一个本地linux小主机获取数据自动上传到阿里云就解决了。近期又发现,更...

建站相关 微信