尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

UniApp 自定义导航栏

UniApp 自定义导航栏
📅 发布时间:2026/6/21 14:13:53

xz-navBar 自定义导航栏

Props

属性名 类型 是否必填 默认 可选 说明
title String false '' 无 导航栏标题
titleAlign String false center center(居中)、left(左边)、right(右边) 导航栏标题对齐方式
navBarStyle Object false {} 无 导航栏自定义样式
navBarBottomType String false border border(边框)、shadow(阴影) navBar下边框显示类型

Emits

事件名 接收值 说明
getNavHeight height 接收navBar元素的高度

Slots

插槽 说明
navBarLeft 导航栏左边
navBarRight 导航栏右边

代码示例

一级导航样式

const navBarStyle = {filter: 'none',position: 'relative',backgroundColor: 'transparent'
};
<xz-navBar title="背景透明,导航占据空间" titleAlign="left" :navBarStyle="navBarStyle"></xz-navBar>
<xz-navBar title="普通" :navBarStyle="navBarStyle"></xz-navBar>

二级导航样式

<xz-navBar title="二级导航" :navBarStyle="navBarStyle"><template #navBarLeft><view class="left"><uni-icons type="left" size="28"></uni-icons></view></template>
</xz-navBar>

自定义样式

<xz-navBar><template #navBarLeft><view class="city">北京</view></template><template #navBarRight><view class="input-view"><uni-icons class="input-uni-icon" type="search" size="18" color="#999" /><input confirm-type="search" class="nav-bar-input" type="text" placeholder="输入搜索关键词" @confirm="confirm" /></view></template>
</xz-navBar>
.city {/* #ifndef APP-PLUS-NVUE */display: flex;/* #endif */flex-direction: row;align-items: center;justify-content: flex-start;// width: 160rpx;margin-left: 4px;margin-right: 10px;
}.input-view {/* #ifndef APP-PLUS-NVUE */display: flex;/* #endif */flex-direction: row;// width: 500rpx;flex: 1;background-color: #f8f8f8;height: 30px;border-radius: 15px;padding: 0 15px;flex-wrap: nowrap;margin: 7px 0;line-height: 30px;
}.input-uni-icon {line-height: 30px;
}.nav-bar-input {height: 30px;line-height: 30px;/* #ifdef APP-PLUS-NVUE */width: 370rpx;/* #endif */padding: 0 5px;font-size: 12px;background-color: #f8f8f8;
}

注意事项:

  1. 需要在pages.json页面的导航栏样式设置为custom
{"path": "pages/index/index","style": {"navigationBarTitleText": "首页","navigationStyle": "custom"}
},
  1. 使用自定义navBar会遮住页面顶部一个navBar的高度,需要页面添加上边距或者使用scroll-view组件更好的完成页面效果。

scroll-view配合自定义tabBar以及自定义导航

  • xz-tabBar:查看
  • xz-navBar:查看
<script lang="ts" setup>
import { onBeforeMount, ref, watch } from 'vue';/*** @property {String} title 页面标题* @property {String} pageKey 页面key* @property {String} navBarHeight 如果用插槽自定义导航栏则需要传入其高度* @property {String} background 页面背景颜色*/
interface Prop {title: string;pageKey: string;navBarHeight: string;background: string;
}
const props = withDefaults(defineProps<Prop>(), {});// 隐藏tabBar
onBeforeMount(() => {uni.hideTabBar();
});const list = [{text: '首页',pageKey: 'calendar',pagePath: '/pages/home/index',iconPath: '/static/tabBar/home.png',selectedIconPath: '/static/tabBar/home.png'},{text: '我的',pageKey: 'user',pagePath: '/pages/user/index',iconPath: '/static/tabBar/user.png',selectedIconPath: '/static/tabBar/user_select.png'}
];const openPage = (url: string) => {uni.switchTab({url});
};// 计算页面高度
let scrollHeight = ref<number>(0);
let navBarHeight = ref<number>(0);
let tabBarHeight = ref<number>(0);
const getNavHeight = (height: number) => {navBarHeight.value = height;
};
const getTabHeight = (height: number) => {tabBarHeight.value = height;
};watch(() => props.navBarHeight,(newVal) => {if (newVal != '0') navBarHeight.value = Number(newVal);},{deep: true,immediate: true}
);
</script><template><view class="page_common" :style="{ background }"><!-- navBar --><slot name="navBar" v-if="$slots.navBar"></slot><xz-navBar v-else :navBarStyle="{ position: 'relative' }" :title="props.title" @getNavHeight="getNavHeight"></xz-navBar><!-- 主体内容 --><scroll-view scroll-y :style="{ height: `calc(100vh - ${navBarHeight + tabBarHeight}px)` }"><view class="page_content"><slot></slot></view></scroll-view><!-- tabBar --><xz-tabBar:tabBarStyle="{ position: 'relative' }":list="list":pageKey="props.pageKey"color="#444444"selectColor="#00B386"tabBarTopType="shadow"@openPage="openPage"@getTabHeight="getTabHeight"></xz-tabBar></view>
</template><style lang="scss" scoped>
.page_common {height: 100vh;overflow: hidden;background-color: rgba(238, 238, 238, 0.2);.page_content {overflow: hidden;box-sizing: border-box;}
}
</style>

相关新闻

  • NOIP2024复盘
  • 题解:CF351B Jeff and Furik
  • js和vue的数据类型

最新新闻

  • Ghost CMS生产环境接管指南:从DigitalOcean一键部署到稳定运维
  • Debian 8 安装 Java 的三大可行方案:apt/离线/二进制免装
  • Codex Subagent 配置深度指南:从 config.toml 到生产级中文智能体
  • NXP Wi-Fi芯片802.11k/v/r无缝漫游实战:从协议原理到工程调试
  • ZXPInstaller完全指南:3分钟解决Adobe插件安装难题的免费方案
  • Transformer架构创新:三阶段残差流设计原理与实现

日新闻

  • 2026速览惠州叛逆青少年学校前十大排名名单出炉 - 武汉中职最新信息发布
  • 2026上饶白蚁消杀哪家好?15年本土2大权威白蚁防治公司推荐(金盾虫控/青蚁卫士) - 我叫一
  • 天龙八部单机版终极数据管理工具:5个技巧快速掌握游戏数据编辑

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号