微信小程序自定义导航栏,胶囊菜单按钮高度适配问题

devtools/2025/3/1 12:38:55/

 抽离公共方法用条件编译对小程序>微信小程序,抖音小程序适配

 公共组件模块建立一个导航模块

<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">标题</view><view class="search"><uni-icons class="icon" type="search" size="18" color="#888"></uni-icons><text class="text">搜索</text></view></view></view><view class="fill" :style="{height:getNavBarHeight()+'px',marginLeft:getLeftIconLeft()+'px'}"></view></view>
</template><script setup>import { ref } from 'vue';import { getStatusBarHeight,getTitleBarHeight,getNavBarHeight,getLeftIconLeft } from '@/utils/system.js';// let SYSTEM_INFO = uni.getSystemInfoSync();// // 状态栏高度// let statusBarHeight = ref(SYSTEM_INFO.statusBarHeight);// // 胶囊按钮状态 解构// let {top,height} = uni.getMenuButtonBoundingClientRect();// let titleBarHeight = ref( height + (top - statusBarHeight.value)-2);// console.log(titleBarHeight);// console.log(top,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{// border: 1px solid red;}.titleBar{display: flex;padding: 0 30rpx;align-items: center;// border: 1px solid green;.title{font-size: 22px;font-weight: 700;color: $text-font-color-1;}.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;.text{// display: flex;align-items: center;padding-left: 10rpx;}.icon{margin-left: 5rpx;}}}}.fill{}
}
</style>

重新在项目下建立一个 utils文件夹 ,再建立一个获取系统信息的js文件,写逻辑代码

// 获取系统信息
const SYSTEM_INFO = uni.getSystemInfoSync();// 获取状态栏高度,若未获取到则默认为0
export const getStatusBarHeight = () => SYSTEM_INFO.statusBarHeight || 10 ;
// 获取标题栏高度
export const getTitleBarHeight = ()=>{if(uni.getMenuButtonBoundingClientRect){// 获取胶囊按钮的位置和尺寸信息let {top,height} = uni.getMenuButtonBoundingClientRect();// 计算标题栏高度return height + (top - getStatusBarHeight())*2;}else{// 若无法获取胶囊按钮信息,默认标题栏高度为80return 40;}
};
// 获取导航栏高度,导航栏高度对于状态栏高度加上标题栏高度
export const getNavBarHeight = ()=> getStatusBarHeight()+getTitleBarHeight();// 抖音小程序export const getLeftIconLeft = ()=>{// if(tt.getMenuButtonBoundingClientRect){// 	// 获取胶囊按钮的位置和尺寸信息// 	let {leftIcon:{left,width}} = tt.getMenuButtonBoundingClientRect();// 	// 计算标题栏高度// 	return left + parseInt(width)+100;// }else{// 	// 若无法获取胶囊按钮信息,默认标题栏高度为80// 	return 40;// }// #ifdef MP-TOUTIAO// 获取胶囊按钮的位置和尺寸信息let {leftIncon:{left,width}} = tt.getMenuButtonBoundingClientRect();// 计算标题栏高度return left + parseInt(width);// #endif// #ifdef MP-TOUTIAOreturn 0;// #endif
}

 可以在pages目录下的页面上进行引用


http://www.ppmy.cn/devtools/163618.html

相关文章

微信小程序记录用户在图书详情页面停留时间--即阅读时间,如果超过两小时,则每小时提醒用户一次

在微信小程序中记录用户在图书详情页面的停留时间&#xff0c;并根据条件&#xff08;如超过两小时&#xff09;进行提醒&#xff0c;可以通过以下步骤实现。以下是详细的实现方案&#xff1a; 1. 实现思路 记录进入页面的时间&#xff1a;当用户进入图书详情页面时&#xff0…

SpringSecurity踢出指定用户

SpringSecurity中可以使用 SessionRegistry 的实现类 SessionRegistryImpl 来获取session相关信息&#xff0c;可以通过这个实现类来踢出用户。 SpringSecurity配置 EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {AutowiredISysUser…

MR30系列分布式I/O:高稳定与高精准赋能锂电池覆膜工艺革新

在新能源行业高速发展的背景下&#xff0c;锂电池生产工艺对自动化控制的精准性和可靠性提出了更高要求。作为锂电池生产中的关键环节&#xff0c;覆膜工艺直接关系到电池的绝缘性能、安全性及使用寿命。面对复杂的工艺控制需求&#xff0c;明达技术MR30系列分布式I/O模块凭借其…

【Groovy】流程控制

1 选择结构 Groovy 中选择结构主要包含 if -else、switch 语句&#xff0c;并且可以返回结果。 1.1 if-else def score 85 if (score > 90) {println("优秀") } else if (score > 80) {println("良好") } else if (score > 60) {println("…

【每日刷题】Day184

【每日刷题】Day184 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. 1700. 无法吃午餐的学生数量 - 力扣&#xff08;LeetCode&#xff09; 2. 146. LRU 缓存 - 力扣&a…

SQL Server 视图的更新排查及清除缓存

目录 前言排查方向 前言 获取数据的时候&#xff0c;发现数据少了两个字段值&#xff0c;归根原因是Java中的实体类少写了两个&#xff0c;后续补充上就好了&#xff01; 但也正了解到视图中的刷新原理以及排查机制&#xff0c;如果确认是视图等引起&#xff0c;可结合如下文…

【MyBatis】核心配置文件详解

文章目录 MyBatis核心配置文件详解1.configuration&#xff1a;2.environments&#xff1a;3.environmen&#xff1a;4.transactionManager&#xff1a;5.dataSource&#xff1a;5.1 UNPOOLED&#xff1a;5.2 POOLED&#xff1a;5.3 JNDI&#xff1a; 6. properties7. mapper M…

使用Fuse-DFS挂载文件存储 HDFS-后端存储ceph

1. 编译环境准备 yum install cmake3 ln -s /usr/bin/cmake3 /usr/bin/cmake yum install gcc-c安装挂载依赖 yum -y install fuse fuse-devel fuse-libs执行以下命令&#xff0c;载入FUSE模块 modprobe fuse2. 下载源码包 hadoop-3.3.4-src.tar.gz解压后执行以下命令 打开…