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

uniapp,获取头部高度

头部自定义时候,设置获取安全区域,可以用  uni.getSystemInfoSync();接口。 

 <view class="statusBar" :style="{height:statusBarHeight+'px'}">

let SYS=uni.getSystemInfoSync();
let statusBarHeight=ref(SYS.statusBarHeight)

头部胶囊按钮设置:

<view class="titleBar" :style="{height:titleBarheight+'px'}">

let titleBarheight=ref((top-statusBarHeight.value)*2+height)

案例,头部组件

效果:

<template>
	<view>
		<view class="layout">
			<view class="navbar">
				<view class="statusBar" :style="{height:statusBarHeight+'px'}">
					
				</view>
				<view class="titleBar" :style="{height:titleBarheight+'px'}">
					<view class="title">
						标题
					</view>
					<view class="search">
						<uni-icons type="search" color="#888" size="18" class="icon"></uni-icons>
						<text class="text">搜索</text>
					</view>
				</view>
			</view>
			<view class="fill" :style="{height:statusBarHeight+titleBarheight+'px'}">
				
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
let SYS=uni.getSystemInfoSync();
let statusBarHeight=ref(SYS.statusBarHeight);
let {top,height}=uni.getMenuButtonBoundingClientRect();
let titleBarheight=ref((top-statusBarHeight.value)*2+height)
</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;
			border: 1px solid green;
		
			.title{
				font-size: 22px;
				font-weight: 700;
				color: #000;
			}
			.search{
				width: 220rpx;
				height: 50rpx;
				border-radius: 60rpx;
				background: rgba(255, 255, 255, 0.4);
				border: 1px solid #fff;
				margin-left: 30rpx;
				color: #999;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				.icon{
					margin-left: 5rpx;
				}
				.text{
					padding-left: 10rpx;
				}
			}
		}
	}
}
</style>

优化代码:建立utils文件夹

utils文件夹下建立system.js文件代码

const SYS=uni.getSystemInfoSync();
export const getstatusBarHeight=()=>SYS.statusBarHeight || 0;
export const getTitleBarHeight=()=>{
	if(uni.getMenuButtonBoundingClientRect){
		let {top,height}=uni.getMenuButtonBoundingClientRect();
		return  height+(top-getstatusBarHeight())*2
		
	}else{
		return 60;
	}
}
export const getNavBarHeight=()=>getstatusBarHeight()+getTitleBarHeight()
/* export getLeftIcon=()=>{
	if(tt.getCustomButtonBoundingClientRect){
		let {leftIcon:{left,width}}=tt.getCustomButtonBoundingClientRect()
		return left+parseInt(width)+5
	}else{
		return 0;
	}
	抖音图标
} */

e
// #ifdef MP-TOUTIAO
let {leftIcon:{left,width}}=tt.getCustomButtonBoundingClientRect()
		return left+parseInt(width)+5
// #endif
// #ifndef MP-TOUTIAO
return 0;
// #endif

组件内代码

<template>
	<view>
		<view class="layout">
			<view class="navbar">
				<view class="statusBar" :style="{height:getstatusBarHeight()+'px'}">
					
				</view>
				<view class="titleBar" :style="{height:getTitleBarHeight() + 'px'}" >
					<view class="title">
						标题
					</view>
					<view class="search">
						<uni-icons type="search" color="#888" size="18" class="icon"></uni-icons>
						<text class="text">搜索</text>
					</view>
				</view>
			</view>
			<view class="fill" :style="{height:getNavBarHeight()+'px'}">
				
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { getstatusBarHeight,getTitleBarHeight,getNavBarHeight} from "@/utils/system.js"

</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: 22px;
				font-weight: 700;
				color: #000;
			}
			.search{
				width: 220rpx;
				height: 50rpx;
				border-radius: 60rpx;
				background: rgba(255, 255, 255, 0.4);
				/* border: 1px solid #fff; */
				margin-left: 30rpx;
				color: #999;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				.icon{
					margin-left: 5rpx;
				}
				.text{
					padding-left: 10rpx;
				}
			}
		}
	}
}
</style>


http://www.kler.cn/news/357229.html

相关文章:

  • Elasticsearch 实战应用与优化策略研究
  • 一些关于FMEA在供应链风险管理中的实际应用案例_SunFMEA
  • 游戏逆向基础-找释放技能CALL
  • 【工具】使用perf抓取火焰图
  • 【4046倍频电路】2022-5-15
  • freeswitch-esl 实现保持通话功能
  • 微服务架构 --- 使用RabbitMQ进行异步处理
  • presence_of_element_located() takes 1 positional argument but 2 were given
  • [LeetCode] 542. 01矩阵
  • Frp 在云服与内网服务器间实现端口映射
  • 【Codeforces】CF 2009 F
  • 【云原生】Docker 部署 Nacos使用详解
  • DFS算法经典题目: Leetcode 51.N皇后
  • 工厂生成中关于WiFi的一些问题
  • 026 elasticsearch文档管理(添加、修改、删除、批处理)-Java原生客户端
  • 力扣1011.在D天内送达包裹的能力
  • Day13-数据库服务架构集群
  • 零基础入门人工智能,如何利用AI工具提升你的学习效率?
  • 父母教养方式测试:理解与优化家庭教育的关键
  • 基于Matlab车牌识别课程设计报告