uniapp小程序--头部处理

uniapp小程序头部状态处理

js:
    // 系统信息  应对手机头部摄像头等的处理
	const SYSTEM_INFO = uni.getSystemInfoSync();
	// 获取状态栏高度 小程序存在
	const statusBarHeight = ref(SYSTEM_INFO.statusBarHeight|| 0 )
	
	
	// 获取胶囊信息  仅小程序存在    --抖音小程序在左上角会有一个功能框、需额外处理
	const titleBarHeight = ref(0);
	//#ifdef/#ifndef 应对多端编译的差异
	// #ifdef MP
	let {top,height} = uni.getMenuButtonBoundingClientRect();
	titleBarHeight.value = (height + (top - statusBarHeight.value) * 2)
	// #endif
	// #ifndef MP
	titleBarHeight.value = 60
	// #endif
	
在视图上使用:style进行高度处理:
<template>
	<view class="layout">
		<view class="navbar">
			<!-- 小程序头部空格空间 -->
			<view class="statusBar" :style="{height:statusBarHeight+ 'px'} "></view>
			<!-- 头部主要部分  名称与搜索 -->
			<view class="titleBar" :style="{height:titleBarHeight+ 'px'}">
				<view class="title">{{title}}</view>
					<view class="search">
						<uni-icons type="search" color="#8888" size="18"></uni-icons>
						<text>搜索</text>
					</view>
				</view>
			</view>
		<!-- 设置高度  应对下方部分内容被遮盖 -->
		<view class="fill" :style="{height:(statusBarHeight + titleBarHeight) + 'px'}"></view>
		
	</view>
</template>

将处理步骤进行封装

封装的js文件
// 系统信息
	const SYSTEM_INFO = uni.getSystemInfoSync();
	
	// 获取状态栏高度 小程序存在
	export const getStatusBarHeight = () => SYSTEM_INFO.statusBarHeight|| 0 ;
	
	// 获取内容高度  最好与头部胶囊按钮高度一直
	export const getTitleBarHeight = () => {
		if(uni.getMenuButtonBoundingClientRect){
			// 获取胶囊信息
			let {top,height} = uni.getMenuButtonBoundingClientRect();
			return height + (top - getStatusBarHeight()) * 2;
		}else{
			return 60;
		}
	}
		// 获取去除胶囊距离(宽度+右边距)的宽度
	export const  getNavBarWidth = () => {
		let width = SYSTEM_INFO.windowWidth || 375;
		// #ifdef MP
			if(uni.getMenuButtonBoundingClientRect()){
				// 获取胶囊信息
				let {left} = uni.getMenuButtonBoundingClientRect();
				return left ;
			}else{
				return 375;
			}
		// #endif
		// #ifndef MP
		  return width;
		// #endif
	}
	
	// 获取导航头部总高度
	export const getNavBarHeight = () => getStatusBarHeight() + getTitleBarHeight();
	
在页面中的使用
<template>
	<view class="layout">
		<view class="navbar">
			<!-- 小程序头部空格空间 -->
			<view class="statusBar" :style="{height:getStatusBarHeight()+ 'px'} "></view>
			<!-- 头部主要部分  名称与搜索 -->
			<view class="titleBar" :style="{height:getTitleBarHeight()+ 'px'}">
				<view class="title">{{title}}</view>
					<view class="search">
						<uni-icons type="search" color="#8888" size="18"></uni-icons>
						<text>搜索</text>
					</view>
				</view>
			</view>
		<!-- 设置高度  应对下方部分内容被遮盖 -->
		<view class="fill" :style="{height:getNavBarHeight()+ 'px'}"></view>
		
	</view>
</template>

<script setup>
	import {getNavBarHeight,getStatusBarHeight,getTitleBarHeight} from '@/utils/babel-header.js'
</script>

在vue2中使用需要将方法先在页面中进行调用,例如created||mounted||computer中调用

<template>
	<view class="layout">
		<view class="navbar">
			<view class="statusBar" :style="{height:StatusBarHeight + 'px'}"></view>
			<view class="titleBar">
				<view class="content">
					title   {{StatusBarHeight}}
				</view>
				<view class="menuButton"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getStatusBarHeight} from "@/utils/babel-header.js"
	export default {
		name:"index-header",
		data() {
			return {
				StatusBarHeight:0,
			};
		},
		created(){
			this.StatusBarHeight = getStatusBarHeight()
			console.log(this.StatusBarHeight);
		}
	}
</script>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值