uni-app 封装刘海状态栏(适用小程序, h5, 头条小程序)

一. 创建一个hooks

hooks--->useSystemBar.js

二. useSystemBar.js

其中// #ifdef MP-WEIXIN 不是注释 这是uni-app的写法

javascript 复制代码
import {ref} from 'vue';

export default function() {
	// 获取系统信息
	let systemInfo = '';
	// #ifdef MP-WEIXIN
		systemInfo = uni.getWindowInfo(); 
	// #endif
	// #ifndef MP-WEIXIN
		systemInfo = uni.getSystemInfoSync();
	// #endif
	
	// 获取刘海-状态栏高度
	let StatusBarHeight = systemInfo.statusBarHeight || 15;
	
	
	// 获取title-自定义内容高度
	let TitleBarHeight = '';
	if(uni.getMenuButtonBoundingClientRect){
		let {top,height} = uni.getMenuButtonBoundingClientRect();
		TitleBarHeight = height + (top - StatusBarHeight)*2		
	}else{
		TitleBarHeight = 40;
	}
	
	// 占位高度-胶囊离下面内容高度
	let NavBarHeight = StatusBarHeight + TitleBarHeight;
	
	// 判断是否是头条
	let ttLeftIconLeft = '';		// 头条胶囊左侧有一个固定的图标,所以要获取图标加图标左侧间距的距离
	// #ifdef MP-TOUTIAO
		let {leftIcon:{left,width}}  = tt.getCustomButtonBoundingClientRect();
		ttLeftIconLeft = left+ parseInt(width);
	// #endif
	// #ifndef MP-TOUTIAO
		ttLeftIconLeft = 0;
	// #endif
	
	
	return {
		StatusBarHeight,
		TitleBarHeight,
		NavBarHeight,
		ttLeftIconLeft
	}
}

三. 使用

在需要页面上使用 下面这个文件是我的项目头部封装 你可以在titleBar标签内写自己的业务逻辑, 并且你拿到StatusBarHeight, TitleBarHeight, NavBarHeight, ttLeftIconLeft这四个值, 你就可以随意操作了

javascript 复制代码
<template>
	<!-- bar -->
	<view class="layout">
		<view class="navbar">
			<!-- 刘海位置-状态栏 -->
			<view class="statusBar" :style="{height: StatusBarHeight + 'px'}"></view>
			<!-- 自己内容位置 -->
			<view class="titleBar" :style="{height:TitleBarHeight+'px',marginLeft:ttLeftIconLeft + 'px'}">
				<view class="title">{{ title }}</view>
				<view class="search">
					<uni-icons class="icon" type="search" color="#888" size="18"></uni-icons>
					<text class="text">搜索</text>
				</view>
			</view>
		</view>
		<!-- 占位 -->
		<view class="fill" :style="{height:NavBarHeight +'px'}"></view>
	</view>
</template>

<script setup>
	defineProps({
		title: {
			type:String,
			default:"壁纸"
		}
	})
	
	// hooks
	import useSystemBar from '../../hooks/useSystemBar';
	const { StatusBarHeight, TitleBarHeight, NavBarHeight, ttLeftIconLeft } = useSystemBar();
	
	
</script>

<style lang="scss" scoped>
.layout{
	.navbar{
		position: fixed;
		top:0;
		left:0;
		width: 100%;
		z-index: 10;
		background:
		linear-gradient(to bottom,transparent,#fff 400rpx),
		linear-gradient(to right,#beecd8 20%,#F4E2D8);
		.statusBar{}
		.titleBar{
			display: flex;	
			align-items: center;
			padding:0 30rpx;
				
			.title{
				font-size: 44rpx;
				font-weight: 700;
				color: $text-font-color-1;
			}
			.search{
				  width: 220rpx;
				  height: 60rpx;
				  border-radius: 60rpx;
				  background: rgba(255,255,255,0.4);
				  margin-left:30rpx;
				  color:#999;
				  font-size: 28rpx;
				  display: flex;
				  align-items: center;
				  .icon{
					  margin-left:5rpx;
				  }
				  .text{
					  padding-left:10rpx;
				  }
			}
		}
	}
	.fill{
		
	}
}
</style>
相关推荐
翻滚吧键盘18 分钟前
vue绑定一个返回对象的计算属性
前端·javascript·vue.js
秃了也弱了。37 分钟前
Chrome谷歌浏览器插件ModHeader,修改请求头,开发神器
前端·chrome
乆夨(jiuze)1 小时前
记录H5内嵌到flutter App的一个问题,引发后面使用fastClick,引发后面input输入框单击无效问题。。。
前端·javascript·vue.js
The_era_achievs_hero1 小时前
微信小程序41~50
微信小程序·小程序
忧郁的蛋~1 小时前
HTML表格导出为Excel文件的实现方案
前端·html·excel
小彭努力中1 小时前
141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
前端·javascript·vue.js·交互
然我2 小时前
别再只用 base64!HTML5 的 Blob 才是二进制处理的王者,面试常考
前端·面试·html
NanLing2 小时前
【纯前端推理】纯端侧 AI 对象检测:用浏览器就能跑的深度学习模型
前端
呆呆的心2 小时前
前端必学:从盒模型到定位,一篇搞定页面布局核心 🧩
前端·css
小飞悟2 小时前
前端高手才知道的秘密:Blob 居然这么强大!
前端·javascript·html