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

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 MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.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-ALIPAY
					Vue.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.kler.cn/news/160753.html

相关文章:

  • 使用函数计算,数禾如何实现高效的数据处理?
  • 系统思考与啤酒游戏经营沙盘
  • Spring Cloud Stream 4.0.4 rabbitmq 发送消息多function
  • 数据库原理: 笛卡儿积
  • 天池SQL训练营(二)-SQL基础查询与排序
  • 用 C 写一个卷积神经网络
  • 2024 年 20 款最佳免费视频转换器软件 [安全快速有效]
  • 【头歌系统数据库实验】实验2 MySQL软件操作及建库建表建数据
  • c++函数模板STL详解
  • Office Tool Plus 使用教程 让个人也能轻松使用上免费的Office
  • 网上商城、宠物商城源码(Java)
  • Vue开发中,在实现单页面应用(SPA)前端路由时的hash模式和history模式的区别及详细介绍
  • MySQL五 | 事务
  • matlab 最小二乘拟合空间直线(方法二)
  • PyTorch 基础篇(1):Pytorch 基础
  • GNSEC 2022年第8届全球下一代软件工程线上峰会-核心PPT资料下载
  • linux里source、sh、bash、./有什么区别
  • AIGC: 关于ChatGPT中API接口调用相关准备工作
  • vue中shift+alt+f格式化防止格式掉其它内容
  • kubeadm 安装k8s1.28.x 底层走containerd 容器
  • Sprite Editor图片编辑器的使用_unity基础开发教程
  • Talk | UCSB博士生欧阳思琦: 利用词级别对比学习实现低资源下的语音翻译
  • [Geek Challenge 2023] web题解
  • [React]基于Antd的FormModal的组件封装以及useFormModal的hooks封装
  • 动态规划学习——最长回文子序列,让字符串变成回文串的最小插入次数
  • swiper和video实现抖音刷视频功能
  • P1005 [NOIP2007 提高组] 矩阵取数游戏
  • 内衣洗衣机哪个牌子好用?家用小型洗衣机推荐
  • 296_C++_一个dialog对话框在执行exec向系统发送一个延后销毁事件时,另一个对话框立刻接管了上一个对话框的销毁事件,导致死UI
  • 定时器的使用及实现