当前位置: 首页 > news >正文

uniapp自定义导航,全端兼容

我们在用uniapp 开发应用的时候,有的页面需要自定义导航,

1.如果普通的直接使用uni 扩展柜组件的 uni-nav-bar 也基本够用,

2.如果稍微带点自定义的这个值无法支持的,特别在小程序端,胶囊是会压住右边的按钮的

自定义个写 支持插槽

不带插槽的直接使用 自定义值看props里面的
小程序端
在这里插入图片描述
h5 app端
在这里插入图片描述

1.新建组件 目录结构 components/NavBar.vue

NavBar.vue 文件
<template><view> <view class='navbar' :style="{'backgroundColor':bgcolor,'z-index':zindex}"><view :style="{'height':tops+'px'}"></view><view :style="{'height':height+'px','line-height':height+'px'}"><view class='mainbox' :style="{'width':widtH+'px','height':'100%'}"><slot name="lf" :style="{'height':height+'px'}" v-if='isBack'><view class="nav-bar-lf"><uni-icons :type="licon" size="25" :color="titColor" @click="goBack"></uni-icons></view></slot><slot name="lc" :style="{'height':height+'px','color':titColor}"><view class="nav-bar-lc" :style="{'color':titColor}">{{title}}</view></slot><slot name="lr" :style="{'height':height+'px'}" v-if='isRbtn'><view class="nav-bar-lr"><uni-icons :type="ricon" size="25" :color="titColor" @click="handRbtn"></uni-icons></view></slot></view></view></view></view>
</template>
<script>export default {props: {title: {// 标题文字(默认为空)type: String,default: "",},titColor: {// 标题和返回按钮颜色(默认白色)type: String,default: "#999",},//建议使用background  因为使用backgroundColor,会导致不识别渐变颜色bgcolor: {// 背景颜色type: String,default: "#f4f4f4",},zindex: {// 层级type: Number,default: 1,},isBack: {// 是否显示返回按钮type: Boolean,default: true,},isRbtn: {// 是否显示右边按钮type: Boolean,default: false,},// 图标licon: {// 返回按钮图标type: String,default: "left",},ricon: {// 右边按钮图标type: String,default: "search",},},data() {return {height: '',widtH: '',tops: ''}},created() {// #ifdef  MPuni.getSystemInfo({success: (e) => {// 计算安全高度this.tops = e.statusBarHeight;let custom = uni.getMenuButtonBoundingClientRect();// 标题栏高度this.height = custom.height + (custom.top - e.statusBarHeight) * 2;// 计算标题栏减去 胶囊的宽度this.widtH = e.windowWidth - custom.width - 10}})// #endif},methods: {goBack() {uni.navigateBack({delta: 1 // 返回的页面数})},// 搜索handRbtn() {this.$emit("onRight")}}}
</script><style>.navbar {width: 100%;position: fixed;top: 0px;}.mainbox {display: flex;align-items: center;/* #ifdef   H5 || APP */height: 45px !important;line-height: 45px;/* #endif */}.nav-bar-lf {width: 45px;height: 100%;text-align: center;}.nav-bar-lc {flex: 1;height: 100%;text-align: center;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.nav-bar-lr {width: 50px;height: 100%;text-align: center;}
</style>

页面中使用

index.vue

<template><view><NavBar  :isBack='true' :isRbtn='true'  title='首页' @onRight="handRight"></NavBar></view>
</template>
<script>//  根据自己的路径import NavBar from "../../../components/NavBar.vue" export default {components: {NavBar},data() {return {}},onLoad() {},methods: {handRight(){console.log("点击右边")}}}
</script><style></style>

带插槽的 ,如果带插槽的 里面样式自己想怎么写怎么写

各端展示如下
在这里插入图片描述
在这里插入图片描述
代码如下 index.vue

<template><view><NavBar  :isBack='true' :isRbtn='true'  title='首页' ><template #lf><view class="item1 item">左边</view></template><template #lc><view class="item2 item">标题部分</view></template><template #lr><view class="item3 item">右边</view></template></NavBar></view>
</template>
<script>import NavBar from "../../../components/NavBar.vue"export default {components: {NavBar},data() {return {}},onLoad() {},methods: {}}
</script><style>
.centermain{flex:1;color:#fff;display: flex;align-items: center;
}
.item{flex: 1;background: pink;
}
.item1{flex: 1;background: pink;
}
.item2{flex: 1;background: greenyellow;
}
.item3{flex: 1;background: salmon;
}
</style>
http://www.lryc.cn/news/456635.html

相关文章:

  • [论文阅读] DVQA: Understanding Data Visualizations via Question Answering
  • 【PostgreSQL】实战篇——数据备份和恢复的最佳实践和工具
  • 代码随想录算法训练营第二十九天|93.复原IP地址 78.子集 90.子集II
  • 【mysql】使用AbstractRoutingDataSource实现多数据源 与 获取mapper上注解
  • 希沃冰点还原
  • Hadoop服务端口号、Spark端口号、Hive端口号以及启动命令
  • 【C++】--类和对象(3)
  • 国外电商系统开发-运维系统文件上传-高级上传
  • 【MongoDB】mongodb | 部署 | 常用命令
  • 【Chrome浏览器插件--资源嗅探猫抓】
  • 2.4Mybatis——缓存机制
  • 移动技术开发:文件的读取
  • Linux 中的 Makefile 伪目标详解
  • Java基础(中)
  • Leetcode热题100-200 岛屿数量
  • 大数据新视界 --大数据大厂之 GraphQL 在大数据查询中的创新应用:优化数据获取效率
  • swift使用代码结构解析
  • 五、Python基础语法(程序的输入和输出)
  • 【C语言】常见概念
  • Electron应用创建和打包
  • 代码随想录算法训练营第五六天| 99. 岛屿数量 100. 岛屿的最大面积
  • 图解 微信开发者工具 小程序源码 调试、断点标记方法 , 微信小程序调试器,真机调试断点调试方法,小程序网络API请求调试方法 总结
  • 注释,换行,控制台输入输出,命名空间,省略return语句
  • 宠物空气净化器该怎么选?希喂,小米、安德迈这三款好用吗?
  • 【Mybatis篇】Mybatis的注解开发
  • NEC协议
  • Meta 发布 Quest 3S 头显及 AR 眼镜原型:开启未来交互新视界
  • 【CSS】水平垂直居中
  • 欧盟零毁林法案 EUDR
  • 26.删除有序数组中的重复项