当前位置: 首页 > news >正文

uni-app 微信小程序之自定义navigationBar顶部导航栏

文章目录

  • 1. 实现效果
  • 2. App.vue
  • 3. pages.json 配置自定义
  • 4. 顶部导航栏 使用

微信小程序自定义 navigationBar 顶部导航栏,兼容适配所有机型

1. 实现效果

在这里插入图片描述

2. App.vue

  1. 在App.vue 中,设置获取的 StatusBarCustomBar 高度(实现适配高度写法)
  2. uni.getSystemInfo 获取可视区域的高度
  3. wx.getMenuButtonBoundingClientRect() 胶囊按钮位置信息
<script>import Vue from 'vue'export default {onLaunch: function() {uni.getSystemInfo({success: function(e) {// #ifndef MPVue.prototype.StatusBar = e.statusBarHeight;if (e.platform == 'android') {Vue.prototype.CustomBar = e.statusBarHeight + 50;} else {Vue.prototype.CustomBar = e.statusBarHeight + 45;};// #endif// #ifdef MP-WEIXINVue.prototype.StatusBar = e.statusBarHeight;let custom = wx.getMenuButtonBoundingClientRect();Vue.prototype.Custom = custom;Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight + 4;// #endif		// #ifdef MP-ALIPAYVue.prototype.StatusBar = e.statusBarHeight;Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;// #endif}});},onShow: function() {console.log('App Show')},onHide: function() {console.log('App Hide')}}
</script><style lang="scss">
</style>

3. pages.json 配置自定义

"pages": [{"path": "home","style": {"navigationStyle": "custom"}
}]

4. 顶部导航栏 使用

  1. 组件完整代码(CSS 部分 我使用的是 colorUI ,不懂的可以看看写法)
<template><view><view class="cu-custom" :style="[{height:CustomBar + 'px'}]"><view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]"><view class="action" @tap="BackPage" v-if="isBack"><text class="cuIcon-back"></text><slot name="backText"></slot></view><view class="content" :style="[{top:StatusBar + 'px'}]"><slot name="content"></slot></view><slot name="right"></slot></view></view></view>
</template><script>export default {data() {return {StatusBar: this.StatusBar,CustomBar: this.CustomBar};},name: 'cu-custom',computed: {style() {var StatusBar = this.StatusBar;var CustomBar = this.CustomBar;var bgImage = this.bgImage;var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;if (this.bgImage) {style = `${style}background-image:url(${bgImage});`;}return style}},props: {bgColor: {type: String,default: ''},isBack: {type: [Boolean, String],default: false},bgImage: {type: String,default: ''},},methods: {BackPage() {uni.navigateBack({delta: 1});}}}
</script><style lang="scss" scoped>
</style>
  1. main.js 全局注册顶部导航栏组件
// 此处为引用自定义顶部
import cuCustom from './colorui/components/cu-custom.vue'
Vue.component('cu-custom',cuCustom);
  1. 页面使用导航栏组件
<cu-custom class="navBox" bgColor="bg-gradual-blue" :isBack="true"><block slot="backText">返回</block><block slot="content">首页</block>
</cu-custom>
http://www.lryc.cn/news/257005.html

相关文章:

  • 前端入门:HTML初级指南,网页的简单实现!
  • 低多边形3D建模石头材质纹理贴图
  • 【华为OD题库-081】最长的元音子串长度-Java
  • 第9节:Vue3 指令
  • B028-JDBC基础
  • ngixn 准备
  • 生活小记录
  • Diary22-全网最全的CSS3.0讲解
  • LAMP和分离式LNMP部署
  • 基于Java房屋租赁管理系统
  • windows安装protoc、protoc-gen-go、protoc-gen-go-grpc
  • macOS 获取文件夹大小
  • Ultimate VFX
  • 一个通用游戏后台的设计模式实践总结
  • Kubernetes - 为什么 K8S 在容器里不能调用自己?
  • 电机:有刷直流电机的原理
  • 小黑子——springBoot基础
  • Flink流批一体计算(24):Flink SQL之mysql维表实时关联
  • 鸿蒙(HarmonyOS)应用开发——从网络获取数据(题目答案)
  • 力扣:197. 上升的温度(Python3)
  • uniApp应用软件在运行时,不符合华为应用市场审核标准。解决方案合集!
  • c#编码技巧(十五):新语法糖record深入分析
  • Java IO流(五)(字符集基础知识简介)
  • 周周爱学习之Redis重点总结
  • 免费的SEO外链发布工具,提升排名的利器
  • 腾讯字节常考的linux命令
  • JAVA后端自学技能实操合集
  • C++ 关联容器
  • ES6之函数新增的扩展
  • postgresql安装部署(docker版本)