微信小程序自定义顶部导航栏(适配各种机型)

效果图

1.pages.js,需要自定义导航栏的页面设置"navigationStyle": "custom"

2.App.vue,获取设备高度及胶囊位置

javascript 复制代码
  onLaunch: function () {
    // 系统信息
    const systemInfo = uni.getSystemInfoSync()
    // 胶囊按钮位置信息
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect()

    this.globalData.navBarInfo = {
      statusBarHeight: systemInfo.statusBarHeight,
      top: menuButtonInfo.top,
      height: menuButtonInfo.height,
      paddingTop: menuButtonInfo.top + menuButtonInfo.height + menuButtonInfo.top - systemInfo.statusBarHeight,
      windowHeight: systemInfo.windowHeight
    };
  },

3.在需要的地方设置导航栏高度

html 复制代码
<template>
  <view class="customNav" :style="{ ...styleClass, opacity: opacity, backgroundColor: bgcolorString }">
    <slot></slot>
  </view>
</template>
javascript 复制代码
    const state = reactive({
      styleClass: {
        paddingTop: '60px',
        height: '32px',
      }
    })
    onShow(() => {
      const navBarInfo = getApp().globalData.navBarInfo
      state.styleClass = {
        paddingTop: navBarInfo.statusBarHeight + 'px',
        height: (navBarInfo.top - navBarInfo.statusBarHeight) * 2 + navBarInfo.height + 'px',
      }
    })
相关推荐
猫七先生18 分钟前
微信小程序一键登录可行性方案
前端·微信小程序
canglingyue1 小时前
微信小程序截屏与录屏功能详解
微信小程序·小程序
微三云-轩2 小时前
区块链系统:解决549 亿元积分商城是否违法的问题
大数据·小程序·重构·区块链·生活
D155540880584 小时前
电竞护航小程序成品搭建三角洲行动护航小程序开发俱乐部点单小程序成品游戏派单小程序定制
游戏·小程序
说私域14 小时前
兴趣电商内容数据洞察未来市场走向研究——基于开源AI智能名片链动2+1模式S2B2C商城小程序的实践
人工智能·小程序
吴传逞14 小时前
记一次uniapp微信小程序开发scss变量失效的问题
微信小程序·uni-app·scss
wmsj057815 小时前
小程序图片批量保存太麻烦?用这款工具一键搞定,附工具教程!
小程序
说私域17 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序服务提升复购率和转介绍率的研究
人工智能·小程序
服务端技术栈1 天前
历时 1 个多月,我的第一个微信小程序「图片转 Excel」终于上线了!
前端·后端·微信小程序
2501_915921431 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview