uniapp,获取头部高度

embedded/2024/10/20 11:34:45/

头部自定义时候,设置获取安全区域,可以用  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.ppmy.cn/embedded/128982.html

相关文章

问题记录-- 在 Vue2 中动态更新 Select 组件的选项

在 Vue2 中动态更新 Select 组件的选项 在 Vue 开发中&#xff0c;动态更新表单组件的选项是一个常见的需求。特别是在使用 select 组件时&#xff0c;如何确保选项能够实时反映数据的变化是一个值得关注的问题。本文将探讨如何通过方法获取 select 的 options 来解决这一问题…

vuex的store应用

1.在pakage.json加一行 2.和main同级别加一个js文件 import Vue from vue import Vuex from vuexVue.use(Vuex)export default new Vuex.Store({state: {langFlag: new Date().getTime()},mutations: {setLangFlag(state) {state.langFlag new Date().getTime()}} })3.在mai…

自由学习记录(7)

文件的判断是否存在&#xff0c;带上文件自己的名字 XmlSerializer (Person)serializer.Deserialize(reader); 如果出错之后&#xff0c;没有try来接&#xff0c;就会直接程序报错暂停&#xff0c; 有了的话无论如何都会继续正常进final using则是正常 为什么要用 using&a…

CTFHUB技能树之SQL——整数型注入

开启靶场&#xff0c;打开链接&#xff1a; 直接指明是SQL整数型注入&#xff0c;但还是来判断一下 &#xff08;1&#xff09;检查是否存在注入点 1 and 11# 返回正确 1 and 12# 返回错误 说明存在SQL整数型注入 &#xff08;2&#xff09;猜字段数 1 order by 2# 1 order…

《深度学习》Dlib库 人脸应用实例 疲劳监测

目录 一、了解项目 1、脸部关键点 2、实现方法 3、流程 初始化阶段&#xff1a; 视频处理循环&#xff1a; 显示与交互&#xff1a; 二、案例实现 1、完整代码 2、运行结果 一、了解项目 1、脸部关键点 2、实现方法 通过眼睛的纵横比来判断眼睛是否闭合。从而判断人…

RabbitMQ service is already present - only updating service parameters

Windows下卸载RabbitMQ之后,然后重新注册RabbitMQ服务的时候,报错以下信息: D:\software\rabbitmq-server-4.0.2\rabbitmq_server-4.0.2\sbin>D:\software\rabbitmq-server-4.0.2\rabbitmq_server-4.0.2\sbin\rabbitmq-service.bat install RabbitMQ service is already …

【C51】单片机与LED数码管的静态显示接口案例分析

目录 ---案例需求--- 1、电路设计 2、程序 3、元器件清单 4、程序仿真 LED数码管有静态显示和动态显示两种显示方式。静态显示是指无论有多少位LE数码管&#xff0c;其都同处于显示状态。数码管工作于静态显示方式时&#xff0c;各位的共阴极&#xff08;或共阳极&#xf…

基于SpringBoot+Vue+uniapp的在线招聘平台的详细设计和实现

详细视频演示 请联系我获取更详细的演示视频 项目运行截图 技术框架 后端采用SpringBoot框架 Spring Boot 是一个用于快速开发基于 Spring 框架的应用程序的开源框架。它采用约定大于配置的理念&#xff0c;提供了一套默认的配置&#xff0c;让开发者可以更专注于业务逻辑而不…