uniapp 中微信小程序自定义导航 胶囊样式适配

app.vue中获取个高度

<script>
	export default {
		onLaunch() {
			// uni.getSystemInfo 获取设备信息 
			// uni.getMenuButtonBoundingClientRect 获取胶囊数据
			// uni.setStorage 异步本地存储
			uni.getSystemInfo({
				success: (res) => {
					const menuButton = uni.getMenuButtonBoundingClientRect() // 胶囊
					const navBarPadding = (menuButton.top - res.statusBarHeight) * 2
					const statusBarHeight = res.statusBarHeight //状态栏高度
					const navHeight = menuButton.height + navBarPadding // nav高度
					const headerHeight = navHeight + statusBarHeight // 总高度
					uni.setStorage({
						key: 'navData',
						data: {statusBarHeight,navHeight,headerHeight}
					})
					console.log(statusBarHeight, navHeight, headerHeight, ) // 20 40 60 iphone6/7/8
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>

封装导航组件

<template>
	<view>
		<view class="navBar">
			<view class="navBar_statusBar" :style="{height: statusBarHeight + 'px'}"></view>

			<view class="navBar_head" :style="{height: navHeight + 'px',lineHeight: navHeight + 'px'}">
				<view class="navBar_callback">
					<image v-if="callback" @click="callBack" src="../static/vertical/callBack.png"></image>
				</view>
				<p class="navBar_content">{{content}}</p>
			</view>
		</view>

		<view class="navBox" :style="{height: headerHeight + 'px'}"></view>
	</view>

</template>

<script>
	export default {
		props: {
			callback: {
				type: Boolean,
				default: true
			},
			content: {
				type: String,
				default: '首页'
			}
		},
		data() {
			return {
				statusBarHeight: '',
				navHeight: '',
				headerHeight: '',
			};
		},

		created() {
			let _this = this
			uni.getStorage({
				key: 'navData',
				success({ data }) {
					_this.statusBarHeight = data.statusBarHeight
					_this.navHeight = data.navHeight
					_this.headerHeight = data.headerHeight
				}
			});
		},
		methods: {
			callBack() {
				uni.navigateBack({

				})
			}
		}
	}
</script>

<style lang="scss">
	.navBar {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		background-color: $green;
		width: 750rpx;

		.navBar_head {
			padding: 0 56rpx;
			display: flex;
			align-items: center;
			height: 53rpx;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 53rpx;
			text-align: center;

			.navBar_callback {
				position: absolute;
				left: 56rx;

				image {
					width: 30rpx;
					height: 30rpx;
				}
			}

			.navBar_content {
				width: 100%;
			}
		}
	}

	.navBox {
		width: 750rpx;
	}
</style>

您的点赞,就是对我的最大支持!!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝知芳誉亘千乡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值