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

uniapp使用vue3语法构建自定义导航栏,适配小程序胶囊

具体代码

<template><view class="nav-wrapper-container" :style="'height:'+navBarHeight +'px'"><view class="nav-status-container" :style="'height:'+navstatusBarHeight +'px;'" /><view v-if="isCustom" class="nav-content-container" :style="'height:'+navContentHeight +'px;'"><slot name="left"></slot><slot name="middle"> </slot><view :style="'width:'+navPaddingRight+'px;'+'height:40px'"></view></view><view v-else class="nav-content-container" :style="'height:'+navContentHeight +'px;'"><image v-if="!isInTab" class="nav-content-left" src="../../static/back.png" mode="widthFix"@click="handleClickBack" /><view class="nav-content-middle"><text>{{titleText}}</text></view></view><slot name="bottom" :style="'height:'+bottomComponentHeight +'px;'"></slot></view>
</template><script setup>import {onBeforeMount,ref,defineProps,defineEmits} from 'vue'const emits = defineEmits(['init-height'])/*** 整个导航栏的高度*/const navBarHeight = ref(0)/*** 状态栏高度*/const navstatusBarHeight = ref(0)/*** 内容高度*/const navContentHeight = ref(0)/*** 距离右侧胶囊的padding-right*/const navPaddingRight = ref(0)/*** 是否在tab页*/const isInTab = getCurrentPages().length == 1/*** 获取导航栏尺寸*/const initNavSize = () => {///获取系统信息const {statusBarHeight,uniPlatform} = uni.getSystemInfoSync()///是否支持这个方法const isNoSupportGetMenuButton = (uniPlatform == "app") || (uniPlatform == "web") || (uniPlatform == "mp-lark")///内容高度let contentHeight = 0///计算内容高度if (!isNoSupportGetMenuButton) {///拿到胶囊信息const menuButton = uni.getMenuButtonBoundingClientRect()contentHeight = (menuButton.top - statusBarHeight) * 2 + menuButton.heightnavPaddingRight.value = menuButton.width + 24} else {contentHeight = 48navPaddingRight.value = 24}///赋值状态栏高度navstatusBarHeight.value = statusBarHeight///赋值内容高度navContentHeight.value = contentHeight///总的高度=内容高度+状态栏高度+bottom组件高度console.log("props.bottomComponentHeight is " + props.bottomComponentHeight)console.log("statusBarHeight is " + statusBarHeight)console.log("contentHeight is " + contentHeight)navBarHeight.value = statusBarHeight + contentHeight + parseInt(props.bottomComponentHeight)emits('init-height', navBarHeight.value)}/*** 返回*/const handleClickBack = () => {uni.navigateBack({delta: 1 // 返回的页面数,这里设置为1表示返回上一页});}const props =defineProps({///标题titleText: {type: String,default: ""},///是否使用自定义插槽isCustom: {type: Boolean,default: false},///bottom组件高度bottomComponentHeight: {type: String,default: "0"}})onBeforeMount(() => {initNavSize()})
</script><style lang="less">.nav-wrapper-container {height: var(--status-bar-height);width: 100%;position: fixed;width: 100%;top: 0;background-color: transparent);left: 0;z-index: 2;align-items: center;}.nav-status-container {width: 100%}.nav-content-container {width: 100%;display: flex;position: relative;align-items: center;}.nav-content-left {width: 40rpx;margin-left: 12rpx;}.nav-content-middle {position: absolute;left: 50%;transform: translate(-50%);}
</style>

使用方法:
使用默认配置:

<navbar titleText="这是标题"></navbar>

使用自定义插槽:

		<navbar :isCustom="true" @init-height="initNavHeight" data-eventsync="true" bottomComponentHeight="45"><template v-slot:left><image class="nav-content-left" src="../../static/back.png" mode="widthFix" @click="handleClickBack" /></template><template v-slot:middle><view class="search-bar-middle" @click="handlerClickSearch()"><image src="../../static/search.png" mode="widthFix" style="width: 24rpx"></image><text class="search-bar-middle-text">搜索内容、体系、文章</text></view></template><template v-slot:bottom><classify-menu-bar :tabArr="tabArr" @on-change-tab="onChangeTab" class="classify-top-container"></classify-menu-bar></template></navbar>

一共有三个插槽:
● left: 左侧
● middle:居中
● bottom:固定底部 (需用传递属性,作为底部buttom的高度)

在这里插入图片描述

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

相关文章:

  • wpf、winform 监听USB拔插时触发
  • C语言:指针笔试题
  • 搜维尔科技:Movella旗下的Xsens在人形机器人开发中得到广泛应用
  • k8s学习--kubernetes服务自动伸缩之水平伸缩(pod副本伸缩)HPA详细解释与案例应用
  • Mock数据
  • 【MySQL】性能分析
  • MyBatis插件机制
  • NVIDIA Jetson Linux 35.3.1-开发指南-导言
  • 14. fastLED调色板
  • bugku---misc---赛博朋克
  • vue+elementplus模拟“山野愚人居”简单实现个人博客
  • ComfyUI 完全入门:Refiner精炼器
  • FastAPI操作关系型数据库
  • 数字化那点事:一文读懂智慧城市
  • RabbitMQ-topic exchange使用方法
  • 6-11 函数题:某范围中的最小值
  • Flask基础2-Jinja2模板
  • Serverless 使用OOS将http文件转存到对象存储
  • AcWing 477:神经网络 ← 拓扑排序+链式前向星
  • 鲁教版八年级数学下册-笔记
  • Web前端栅格:深入解析与实战应用
  • mysql Innodb引擎常见问题
  • 创建 MFC DLL-使用关键字_declspec(dllexport)
  • 机器学习笔记 - 用于3D数据分类、分割的Point Net的网络实现
  • C#知识|基于实体类对象,返回实体集合封装介绍。
  • 关于Redis中哨兵(Sentinel)
  • 论文阅读:H-ViT,一种用于医学图像配准的层级化ViT
  • 【MySQL】(基础篇七) —— 通配符和正则表达式
  • HTML静态网页成品作业(HTML+CSS)—— 名人霍金介绍网页(6个页面)
  • MySQL: 索引与事务