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

效果图

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',
      }
    })
相关推荐
從南走到北4 小时前
挪车小程序挪车二维码php+uniapp
微信小程序·小程序·开源·微信公众平台
黑云压城After4 小时前
uniapp小程序自定义日历(签到、补签功能)
小程序·uni-app
黑云压城After5 小时前
小程序(物流、快递),接入GPS北斗获取路线以及当前车辆位置
小程序
万岳科技程序员小金7 小时前
互联网医院系统源码解析:如何开发智能化的电子处方小程序?
小程序·app开发·互联网医院系统源码·智慧医疗小程序·医院app
Java Fans7 小时前
微信小程序——访问服务器媒体文件的实现步骤
服务器·微信小程序·小程序
Evaporator Core10 小时前
微信小程序数据绑定与事件处理:打造动态交互体验
微信小程序·小程序·交互
流烟默1 天前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序
家里有只小肥猫1 天前
uniApp小程序保存canvas图片
前端·小程序·uni-app
低代码布道师1 天前
性格测评小程序10生成报告
低代码·小程序
科技小E1 天前
EasyRTC:基于WebRTC与P2P技术,开启智能硬件音视频交互的全新时代
网络·网络协议·小程序·webrtc·p2p·智能硬件·视频监控