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

uniapp实现自定义导航内容高度居中(兼容APP端以及小程序端与胶囊对齐)

①效果图如下

1.小程序端与胶囊对齐

2.APP端内容区域居中

 

 

注意:上面使用的是colorui里面的自定义导航样式。

②思路:

1.APP端和小程序端走不同的方法,因为小程序端要计算不同屏幕下右侧胶囊的高度。

2.其次最重要的要清晰App端和小程序端的计算逻辑。

3.然后调用api获取屏幕信息,小程序还需要单独调用获取胶囊的api。

系统信息uni.getSystemInfoSync()

小程序端胶囊信息uni.getSystemInfoSync

4.最后写公共的封装方法,在多个页面调用。

小程序端计算方法:

2.1.头部整体高度 ==状态栏高度 + 导航栏高度

2.2.导航栏高度 == (胶囊距顶部高度-状态栏高度) *2 +胶囊高度

2.3.计算导航内容距离顶部高度= 状态栏高度/2

APP端计算方法:

2.4.计算自定义导航栏的高度=((屏幕高度-状态栏高度)/需要除的比例)

③实现代码

3.1、封装的公共的方法APP端和小程序端

	/** 共用的自定义导航高度位置(App端)* 在页面中获取系统信息,并计算自定义导航栏的高度* comNum 计算除数* saveFloat 保留小数位数*/utilsNavbarHeight(screenH, statusH, comNum, saveFloat) {const screenHeight = screenH; // 屏幕高度const statusBarHeight = statusH; // 状态栏高度var saveFloats = 2if (saveFloat != undefined) {saveFloats = saveFloat}// 计算自定义导航栏的高度const navBarHeight = ((screenHeight - statusBarHeight) / comNum).toFixed(saveFloats); // 例如除以10,可以根据实际需求进行调整return navBarHeight},/**小程序端与胶囊平行*/WechatNavBarHeight() {//获取状态栏高度const statusBarHeight = uni.getSystemInfoSync().statusBarHeight//获取小程序胶囊信息const menu = uni.getMenuButtonBoundingClientRect()//导航栏高度 == (胶囊距顶部高度-状态栏高度) *2 +胶囊高度const navBarHeightWechat = (menu.top - statusBarHeight) * 2 + menu.height//头部整体高度 ==状态栏高度 + 导航栏高度const headerHeight = statusBarHeight + navBarHeightWechat//计算导航内容距离顶部高度= 状态栏高度/2const topHeight = statusBarHeight / 2 + 'px'return {topHeight,headerHeight}},

3.2、使用自定义导航栏页面调用

注意:height动态绑定的是navBarHeight,整体导航栏高度

           top动态邦定的是statusBarHeight,计算后的距顶部高度

//布局
<view class="Content"><!-- 自定义导航 --><view class="navbar"><view class="cu-bar bg-blue search" :style="{'height':navBarHeight}"><view class="rowList" :style="{'top':statusBarHeight}"><view class="action" @click="loca"><text>测试</text><text class="cuIcon-triangledownfill"></text></view><view :class="[isWeixin?'search-form radius wechatNavbar':'search-form radius']"><text class="cuIcon-search"></text><input @tap.stop="InputFocus" :disabled="true" :adjust-position="false" type="text":placeholder="currentWord" confirm-type="search"></input></view><view class="cu-avatar round" @click="addFunction":style="isWeixin ? 'background-image:url(static/images/index/add.png)' : 'background-image:url(/static/images/index/add.png)'"></view></view></view></view>
//初始化数据navBarHeight: null,//导航栏高度statusBarHeight: null,//导航内容距离整体导航栏高度headerHeight: null, //顶部导航整体高度//方法
//计算导航栏高度comNavbarHeight() {// #ifdef APP-PLUSconst devres = this.$system.devInfo()const navBarHeight = this.$system.utilsNavbarHeight(devres.screenHeight, devres.statusBarHeight, 8.6, 2)this.navBarHeight = navBarHeight + 'px'this.statusBarHeight = devres.statusBarHeight / 2 + 'px' //14% 准确来说14%this.headerHeight = navBarHeight// #endif// #ifdef MP-WEIXINconst wechatObj = this.$system.WechatNavBarHeight()this.statusBarHeight = wechatObj.topHeightthis.navBarHeight = wechatObj.headerHeight + 'px'this.headerHeight = wechatObj.headerHeight// #endif},

这样就可以了,实现过程中也踩了很多坑,有什么问题评论区留言啊!

http://www.lryc.cn/news/117071.html

相关文章:

  • Python调用外部电商API的详细步骤
  • 什么是NVME
  • 交叉编译驱动和应用出现警告提示错误“cc1:all warnings being treated as errors”解决方法
  • 基于nodejs+vue+uniapp微信小程序的短视频分享系统
  • ElasticSearch:环境搭建步骤
  • 剑指 Offer 37. 序列化二叉树
  • 如何快速完成MySQL数据的差异对比|NineData
  • Vue3项目中将html元素转换为word
  • Unity-Shader-高亮Highlight
  • Linux操作系统(二):操作系统结构与内核设计
  • 小研究 - 领域驱动设计DDD在IT企业内部网站开发中的运用(二)
  • 在Qt中实现鼠标监听与交互
  • 力扣hot100刷题记录
  • 阿里云国际站视频直播服务是什么呢?
  • python实现简单的爬虫功能
  • AI文档识别技术之表格识别 (一)
  • uni-app 支持 app端, h5端,微信小程序端 图片转换文件格式 和 base64
  • 云计算——存储虚拟化简介 与 存储模式及方法
  • 数据资产目录建设之数据分类全解
  • 大模型的数据隐私问题有解了,浙江大学提出联邦大语言模型
  • flask-sqlalchemy使用
  • flask处理token的装饰器
  • 【Express.js】页面渲染
  • 2.UE数字人语音交互(UE数字人系统教程)
  • C语言——水仙花数字
  • java中list对象拷贝至新的list对象并保持两个对象独立的方法
  • 使用AI工具Lama Cleaner一键去除水印、人物、背景等图片里的内容
  • 瑞数系列及顶像二次验证LOGS
  • Anaconda版本和Python版本对应关系(持续更新...)
  • vscode 搭建STM32开发环境