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.height
			navPaddingRight.value = menuButton.width + 24
		} else {
			contentHeight = 48
			navPaddingRight.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: #f3f3f3;
		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.niftyadmin.cn/n/5406830.html

相关文章

在使用spring boot是application文件没绿

点击 File 选中 Project Structure 弹出如下窗口&#xff0c;按图中标记红框进行选择 通过上述操作&#xff0c;会弹出如下窗口 点击上图的 号&#xff0c;弹出选择该模块的配置文件 通过上述几步后&#xff0c;就可以看到如下界面。properties 类型的配合文件有一个&…

14-RPC-自研微服务框架

RPC RPC 框架是分布式领域核心组件&#xff0c;也是微服务的基础。 RPC &#xff08;Remote Procedure Call&#xff09;全称是远程过程调用&#xff0c;相对于本地方法调用&#xff0c;在同一内存空间可以直接通过方法栈实现调用&#xff0c;远程调用则跨了不同的服务终端&a…

梯度和梯度损失

梯度主要用于计算图像融合过程中的梯度损失&#xff0c;对应的损失函数是梯度损失&#xff08;loss_grad&#xff09;。 梯度的作用&#xff1a; 图像的梯度是指图像中每个像素的灰度变化率&#xff0c;通常用于表示图像的边缘和纹理信息。在图像融合任务中&#xff0c;通过计算…

【Boost搜索引擎项目】Day1 项目介绍+去标签和数据清洗框架搭建

&#x1f308;欢迎来到C项目专栏 &#x1f64b;&#x1f3fe;‍♀️作者介绍&#xff1a;前PLA队员 目前是一名普通本科大三的软件工程专业学生 &#x1f30f;IP坐标&#xff1a;湖北武汉 &#x1f349; 目前技术栈&#xff1a;C/C、Linux系统编程、计算机网络、数据结构、Mysq…

ubuntu20.04安装nvidia驱动真实有效(被折磨了一天一夜的肝文!!!)

ubuntu20.04安装nvidia驱动真实有效 安装前后需要注意的安装nvidia驱动的教程 安装前后需要注意的 能找到这篇帖子说明你之前肯定有过无数次方法的尝试&#xff0c;这些尝试可能会影响下面教程的有效 1.下面这个指令可能会导致ubuntu内核的更新。内核更新可能会导致你的nvidia…

xmake常用命令

1.编译 xmake2. 清缓存 这个命令用于清除 xmake 的配置缓存。xmake 在执行 xmake f 命令时会生成一个配置缓存文件&#xff0c;该文件保存了项目的配置信息。通过执行 xmake f -c 命令&#xff0c;可以清除这些缓存文件&#xff0c;以便重新配置项目。 xmake f -c 3.清理 这个…

Flamingo a Visual Language Model for Few-Shot Learning

Flamingo: a Visual Language Model for Few-Shot Learning TL; DR&#xff1a;Flamingo 在 VL-adapter 的结构上有创新&#xff0c;Perceiver Resampler gated xattn&#xff0c;一种看起来比较复杂且高级的将图像特征注入到语言模型的方式。同时&#xff0c;优秀的结构设计…

将六西格玛设计融入汽车制造:实践之路

在快节奏的现代生活中&#xff0c;汽车早已不再仅仅是一种交通工具&#xff0c;而是成为了展现个性、追求品质生活的重要象征。为了满足消费者日益增长的品质需求&#xff0c;汽车制造商们纷纷将目光投向了六西格玛设计这一先进的质量管理方法。那么&#xff0c;如何将六西格玛…