自定義組件 navbarvue
(膠囊底部高度 - 狀態欄的高度) + (膠囊頂部高度 - 狀態欄內的高度) = 導航欄的高度
<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="16" color="#999"></uni-icons>
</view>
<view class="navbar-serach">
<input class="navbar-search_text" type="text" v-model="val" placeholder="請輸入您要搜索的內容" />
</view>
</view>
</view>
</view>
<!-- 需要添加占位符高度 狀態欄高度+導航欄高度(否則下面tab會塌陷)-->
<view :style="{height: statusBarHeight+navBarHeight+'px'}"></view>
</view>
</template>
<script>
export default {
name: 'navbar',
data() {
return {
statusBarHeight: 20,/* 狀態欄高度 */
navBarHeight: 45,/* 導航欄高度 */
windowWidth: 375,/* 窗口寬度 */
/* 設定狀態欄默認高度 */
val: ''/* 導航欄搜索框的值 */
};
},
created() {
// 獲取手機系統信息
const info = uni.getSystemInfoSync()
// 設置狀態欄高度(H5頂部無狀態欄小程序有狀態欄需要撐起高度)
this.statusBarHeight = info.statusBarHeight
this.windowWidth = info.windowWidth
// 除了h5 app mp-alipay的情況下執行
// #ifndef H5 || APP-PLUS || MP-ALIPAY
// 獲取膠囊的位置
const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
console.log(menuButtonInfo);
// (膠囊底部高度 - 狀態欄的高度) + (膠囊頂部高度 - 狀態欄內的高度) = 導航欄的高度
this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
this.windowWidth = menuButtonInfo.left
// #endif
}
}
</script>
<style lang="less">
@import '../../common/css/icons.css';
@import './../../uni.less';
.navbar {
.navbar-fixed {
position: fixed;
top: 0;
left: 0;
z-index: 99;
width: 100%;
background-color: @mk-base-color;
.navbar-content {
display: flex;
justify-content: center;
align-items: center;
padding: 0 15px;
height: 45px;
box-sizing: border-box;
.navbar-search {
display: flex;
align-items: center;
padding: 0 10px;
width: 100%;
height: 30px;
border-radius: 30px;
background-color: #fff;
.navbar-search_icon {
// width: 10px;
// height: 10px;
margin-right: 10px;
}
.navbar-search_text {
width: 100%;
font-size: 14px;
color: #999;
}
}
&.search {
padding-left: 0;
.navbar-content__search-icons {
margin-left: 10px;
margin-right: 10px;
}
.navbar-search {
border-radius: 5px;
}
}
}
}
}
</style>
uni.less文件
/**
* 這里是uni-app內置的常用樣式變量
*
* uni-app 官方擴展插件及插件市場(https://ext.dcloud.net.cn)上很多三方插件均使用了這些樣式變量
* 如果你是插件開發者,建議你使用scss預處理,並在插件代碼中直接使用這些變量(無需 import 這個文件),方便用戶通過搭積木的方式開發整體風格一致的App
*
*/
/**
* 如果你是App開發者(插件使用者),你可以通過修改這些變量來定制自己的插件主題,實現自定義主題功能
*
* 如果你的項目同樣使用了scss預處理,你也可以直接在你的 scss 代碼中使用如下變量,同時無需 import 這個文件
*/
/* 顏色變量 */
@mk-base-color : #f07373;
/* 行為相關顏色 */
@uni-color-primary: #007aff;
@uni-color-success: #4cd964;
@uni-color-warning: #f0ad4e;
@uni-color-error: #dd524d;
/* 文字基本顏色 */
@uni-text-color:#333;//基本色
@uni-text-color-inverse:#fff;//反色
@uni-text-color-grey:#999;//輔助灰色,如加載更多的提示信息
@uni-text-color-placeholder: #808080;
@uni-text-color-disable:#c0c0c0;
/* 背景顏色 */
@uni-bg-color:#ffffff;
@uni-bg-color-grey:#f8f8f8;
@uni-bg-color-hover:#f1f1f1;//點擊狀態顏色
@uni-bg-color-mask:rgba(0, 0, 0, 0.4);//遮罩顏色
/* 邊框顏色 */
@uni-border-color:#c8c7cc;
/* 尺寸變量 */
/* 文字尺寸 */
@uni-font-size-sm:24rpx;
@uni-font-size-base:28rpx;
@uni-font-size-lg:32rpx;
/* 圖片尺寸 */
@uni-img-size-sm:40rpx;
@uni-img-size-base:52rpx;
@uni-img-size-lg:80rpx;
/* Border Radius */
@uni-border-radius-sm: 4rpx;
@uni-border-radius-base: 6rpx;
@uni-border-radius-lg: 12rpx;
@uni-border-radius-circle: 50%;
/* 水平間距 */
@uni-spacing-row-sm: 10px;
@uni-spacing-row-base: 20rpx;
@uni-spacing-row-lg: 30rpx;
/* 垂直間距 */
@uni-spacing-col-sm: 8rpx;
@uni-spacing-col-base: 16rpx;
@uni-spacing-col-lg: 24rpx;
/* 透明度 */
@uni-opacity-disabled: 0.3; // 組件禁用態的透明度
/* 文章場景相關 */
@uni-color-title: #2C405A; // 文章標題顏色
@uni-font-size-title:40rpx;
@uni-color-subtitle: #555555; // 二級標題顏色
@uni-font-size-subtitle:36rpx;
@uni-color-paragraph: #3F536E; // 文章段落顏色
@uni-font-size-paragraph:30rpx;