H5与微信小程序
通过获取系统信息和获取胶囊按钮的信息,得到获取标题栏高度,成而做好自定义头部导航栏
在微信小程序可使用
但在H5就保错,就需要优化
<!-- 全局custom-nav-bar组件 -->
<template>
<view class="customNavBar">
<view class="navbar">
<!-- 状态栏 -->
<view class="statusBar" :style="{height:statusBarHeight + 'px'}"> </view>
<!-- 标题栏 -->
<view class="titleBar" :style="{height:titleBarHeight + 'px'}">
<view class="title">标题</view>
<view class="search">
<uni-icons type="search" color="#888" size="18" class="icon"></uni-icons>
<text class="text">搜索</text>
</view>
</view>
</view>
<!-- 填充区域 -->
<view class="fill" :style="{height:statusBarHeight +titleBarHeight+ 'px'}">
</view>
</view>
</template>
<script setup>
import { ref } from 'vue';
import { onShow } from "@dcloudio/uni-app";
//获取系统信息
let systemInfo = uni.getSystemInfoSync();
//获取状态栏的高度
let statusBarHeight=ref(systemInfo.statusBarHeight)
//获取胶囊按钮的信息
// let menuButton =uni.getMenuButtonBoundingClientRect()
let {top,height}=uni.getMenuButtonBoundingClientRect();
// console.log(top,height,statusBarHeight)//24 32 20
//获取标题栏高度
let titleBarHeight= ref((top-statusBarHeight.value)*2 + height)//40
</script>
<style lang="scss" scoped>
.customNavBar{
.navbar{
position: fixed;
top:0;
left: 0;
width: 100%;
z-index: 20;
background:
linear-gradient(to bottom,rgba(0,0,0,0),#fff 400rpx),
linear-gradient(to right, #beecd8 20%,#F4E2d8);
.statusBar{}
.titleBar{
display: flex;
padding-left: 30rpx;
align-items: center;
.title{
font-size: 22px;
font-weight: 600;
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;
display: flex;
align-items: center;
.icon{
margin-left:5rpx;
}
.text{
padding-left:10rpx;
}
}
}
}
</style>
记得在Layoutindex.vue引入
<view class="layout pageColor">
<custom-nav-bar></custom-nav-bar>
<!-- 轮播图 -->
//....
</view>