uni-app搜索导航栏组件

<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏占位 -->
			<view :style="{height: statusBarHeight+'px'}"></view>
			<!-- 导航栏内容 -->
			<view class="navbar-content" :style="{height:navbarHeight+'px',width:windowWidth+'px'}">
				<view class="navbar-search">
					<view class="navbar-search-icon">
						<uni-icons type="search" size="20" color="#999"></uni-icons>
					</view>
					<view class="navbar-search-text">uni-app</view>
				</view>
			</view>
		</view>
		<!-- 占位状态栏+导航栏的高度,使下面的内容不会被遮挡 -->
		<view :style="{height:(statusBarHeight + navbarHeight)+'px'}"></view>
	</view>
</template>

<script>
	export default {
		name: "navbar",
		data() {
			return {
				statusBarHeight: 20,
				navbarHeight: 45,
				windowWidth: 375
			};
		},
		// 组件加载
		created() {
			// 同步获取手机系统信息
			let info = uni.getSystemInfoSync()
			console.log(info);
			// 动态设置状态栏高度
			this.statusBarHeight = info.statusBarHeight
			// 设置搜索框的宽度
			this.windowWidth = info.windowWidth

			/* 条件编译 不等于这三个平台 , 只会在微信小程序,QQ,百度小程序运行*/
			// #ifndef H5 || MP-ALIPAY || APP-PLUS
			/* 动态设置导航栏的高度 */
			// 获取胶囊位置
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo);
			// 导航栏高度 = (胶囊底部 - 状态栏高度) + (胶囊顶部 - 状态栏高度)
			this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this
				.statusBarHeight)
			console.log(this.navbarHeight);
			// 设置搜索框的宽度
			this.windowWidth = menuButtonInfo.left
			// #endif
		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-fixed {
			// 固定定位
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;
			width: 100%;
			background-color: $mk-base-color;

			.navbar-content {
				display: flex;
				// 水平居中
				justify-content: center;
				// 垂直居中
				align-items: center;
				padding: 0 15px;
				// 盒内显示 (父元素和子元素宽度都是100%,但是父元素加了左右内填充,会导致子元素不在父元素内)
				box-sizing: border-box;

				.navbar-search {
					display: flex;
					// flex-direction默认水平排列
					flex-direction: row;
					// 垂直居中
					align-items: center;
					height: 30px;
					width: 100%;
					border-radius: 30px;
					padding: 0 10px;
					background-color: #FFFFFF;

					.navbar-search-icon {
						margin-right: 10px;
					}

					.navbar-search-text {
						font-size: 12px;
						color: #999;
					}
				}
			}
		}
	}
</style>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值