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

效果图

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',
      }
    })
相关推荐
全职计算机毕业设计13 小时前
基于微信小程序的城市特色旅游推荐应用的设计与实现
微信小程序·小程序
gaojianqiao123417 小时前
uniapp引入七鱼客服微信小程序SDK
微信小程序·uni-app
说私域18 小时前
线下消费经济“举步维艰”,开源AI智能名片链动2+1+S2B2C小程序线上“狂飙突进”!
人工智能·小程序·开源·零售
天上掉下来个程小白19 小时前
添加购物车-02.代码开发
java·服务器·前端·后端·spring·微信小程序·苍穹外卖
JAVA叶知秋1 天前
uniapp自定义底部导航栏h5有效果小程序无效的解决方案
小程序·uni-app
沙尘暴炒饭2 天前
用uniapp在微信小程序实现画板(电子签名)功能,使用canvas实现功能
微信小程序·小程序·uni-app
PyAIGCMaster2 天前
Taro 编译不平不同平台小程序
小程序
fakaifa3 天前
【开源版】likeshop上门家政系统PHP版全开源+uniapp前端
小程序·uni-app·php·家政小程序源码·家政服务小程序·源码下载·上门家政
说私域3 天前
基于定制开发开源AI智能名片S2B2C商城小程序的公私域流量融合运营策略研究
人工智能·小程序·开源·零售
阿諪諪3 天前
uniapp小程序中实现无缝衔接滚动效果
小程序·uni-app