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

效果图

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',
      }
    })
相关推荐
weixin_lynhgworld6 小时前
盲盒抽卡机小程序系统开发:连接线上线下娱乐新桥梁
小程序·娱乐
程知农7 小时前
uniapp_微信小程序_根据胶囊按钮计算出的导航栏高度为什么不是44px?
微信小程序·小程序·uni-app
鲲鹏猿10 小时前
微信小程序——早餐小程序
微信小程序·小程序
说私域10 小时前
基于开源AI智能名片链动2+1模式与S2B2C商城小程序的微商品牌规范化运营研究
人工智能·小程序·开源
2501_9159090610 小时前
iOS 加固工具实战解析,主流平台审核机制与工具应对策略
android·ios·小程序·https·uni-app·iphone·webview
The_era_achievs_hero10 小时前
UniappDay04
vue.js·微信小程序·uni-app
2501_9151063212 小时前
iOS WebView 调试实战,第三方脚本加载失败与内容安全策略冲突问题排查指南
android·ios·小程序·https·uni-app·iphone·webview
paopaokaka_luck17 小时前
基于SpringBoot+Uniapp的健身饮食小程序(协同过滤算法、地图组件)
前端·javascript·vue.js·spring boot·后端·小程序·uni-app
老虎06271 天前
JavaWeb(苍穹外卖)--学习笔记13(微信小程序开发,缓存菜品,Spring Cache)
笔记·学习·微信小程序
郭邯1 天前
小程序中自定义组件的样式隔离
微信小程序