uniApp开辟小步调自界说tabBar底部导航栏+tabBar中心凸起自界说样式实现

[复制链接]
发表于 2026-4-24 09:42:44 | 显示全部楼层 |阅读模式
        先看效果是否可以满意你们,假如可以满意你只要一步一步照着做绝对没有错。
        本人技能不佳,研究了一整天,全网的大佬们写的博客看的晕头转向,克制大伙再走弯路,跟着我以下步调一点一点来绝对可以实现。
        终极效果图: (假如你看着还满意的话那就跟着教程一步一步来吧)

起首你要确保你的项目中安装了 uview的UI框架和vuex,具体安装教程这两个网上都有具体教程,我这项目是Vue3.0的,用的是uview-plus框架。
第一步:设置信息

pages.js 添加 "custom": true 属性

第二步:添加自界说tabBar代码文件

留意:这里是按官方要求必须放在项目根目次下,而且文件名不能修改,index中代码如下:
  1. <template>
  2.         <view>
  3.                 <u-tabbar :value="index" @change="tabBarChange" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true"
  4.                         activeColor="#d81e06">
  5.                         <u-tabbar-item text="首页">
  6.                                 <template #active-icon>
  7.                                         <image class="u-page__item__slot-icon" :src="list[0].selectedIconPath"></image>
  8.                                 </template>
  9.                                 <template #inactive-icon>
  10.                                         <image class="u-page__item__slot-icon" :src="list[0].iconPath"></image>
  11.                                 </template>
  12.                         </u-tabbar-item>
  13.                         <u-tabbar-item text="转让">
  14.                                 <template #active-icon>
  15.                                         <image class="u-page__item__slot-icon" :src="list[1].selectedIconPath"></image>
  16.                                 </template>
  17.                                 <template #inactive-icon>
  18.                                         <image class="u-page__item__slot-icon" :src="list[1].iconPath"></image>
  19.                                 </template>
  20.                         </u-tabbar-item>
  21.                         <u-tabbar-item @click="show = true">
  22.                                 <template #active-icon>
  23.                                         <image class="u-page__item__slot-icon-big" :src="list[2].selectedIconPath">
  24.                                         </image>
  25.                                 </template>
  26.                                 <template #inactive-icon>
  27.                                         <image class="u-page__item__slot-icon-big" :src="list[2].iconPath"></image>
  28.                                 </template>
  29.                         </u-tabbar-item>
  30.                         <u-tabbar-item text="积分">
  31.                                 <template #active-icon>
  32.                                         <image class="u-page__item__slot-icon" :src="list[3].selectedIconPath"></image>
  33.                                 </template>
  34.                                 <template #inactive-icon>
  35.                                         <image class="u-page__item__slot-icon" :src="list[3].iconPath"></image>
  36.                                 </template>
  37.                         </u-tabbar-item>
  38.                         <u-tabbar-item text="我的">
  39.                                 <template #active-icon>
  40.                                         <image class="u-page__item__slot-icon" :src="list[4].selectedIconPath"></image>
  41.                                 </template>
  42.                                 <template #inactive-icon>
  43.                                         <image class="u-page__item__slot-icon" :src="list[4].iconPath"></image>
  44.                                 </template>
  45.                         </u-tabbar-item>
  46.                 </u-tabbar>
  47.                 <view>
  48.                         <u-popup :overlayOpacity="0.6" :round="10" :show="show" @close="close" @open="open">
  49.                                 <view class="issue-item">
  50.                                         <view class="issue-item-buy" @click="toBuy">
  51.                                                 <text>我要卖</text>
  52.                                         </view>
  53.                                         <view class="issue-item-sell">
  54.                                                 <text>我要买</text>
  55.                                         </view>
  56.                                 </view>
  57.                         </u-popup>
  58.                 </view>
  59.         </view>
  60. </template>
  61. <script>
  62.         export default {
  63.                 data() {
  64.                         return {
  65.                                 show: false,
  66.                                 list: [{
  67.                                                 "pagePath": "/pages/index/index",
  68.                                                 "text": "首页",
  69.                                                 "iconPath": "/static/tabs/home_default.png",
  70.                                                 "selectedIconPath": "/static/tabs/home_selected.png"
  71.                                         },
  72.                                         {
  73.                                                 "pagePath": "/pages/makeOver/makeOver",
  74.                                                 "text": "转让",
  75.                                                 "iconPath": "/static/tabs/mass_default.png",
  76.                                                 "selectedIconPath": "/static/tabs/mass_selected.png"
  77.                                         },
  78.                                         {
  79.                                                 "pagePath": "/pages/issue/issue",
  80.                                                 "text": "发布",
  81.                                                 "iconPath": "/static/images/tab_issue.png",
  82.                                                 "selectedIconPath": "/static/images/tab_issue.png"
  83.                                         },
  84.                                         {
  85.                                                 "pagePath": "/pages/integral/integral",
  86.                                                 "text": "积分",
  87.                                                 "iconPath": "/static/tabs/mass_default.png",
  88.                                                 "selectedIconPath": "/static/tabs/mass_selected.png"
  89.                                         },
  90.                                         {
  91.                                                 "pagePath": "/pages/my/my",
  92.                                                 "text": "我的",
  93.                                                 "iconPath": "/static/tabs/my_default.png",
  94.                                                 "selectedIconPath": "/static/tabs/my_selected.png"
  95.                                         }
  96.                                 ]
  97.                         }
  98.                 },
  99.                 computed: {
  100.                         index() {
  101.                                 return this.$store.state.tabbarIndex
  102.                         }
  103.                 },
  104.                 methods: {
  105.                         tabBarChange(e) {
  106.                                 if (e !== 2) {
  107.                                         uni.switchTab({
  108.                                                 url: this.list[e].pagePath
  109.                                         })
  110.                                 }
  111.                         },
  112.                         //点击发布按钮的弹出层
  113.                         open() {
  114.                                 console.log('open');
  115.                         },
  116.                         close() {
  117.                                 this.show = false;
  118.                                 console.log('close');
  119.                         },
  120.                         //点击我要卖
  121.                         toBuy() {
  122.                                 console.log("点击了");
  123.                                 uni.switchTab({
  124.                                         url: '/pages/issue/issue'
  125.                                 })
  126.                         }
  127.                 }
  128.         }
  129. </script>
  130. <style lang="scss">
  131.         .u-page__item__slot-icon {
  132.                 width: 50rpx;
  133.                 height: 50rpx;
  134.         }
  135.         .u-page__item__slot-icon-big {
  136.                 width: 120rpx;
  137.                 height: 120rpx;
  138.                 margin-bottom: 30rpx;
  139.         }
  140.         .issue-item {
  141.                 height: 400rpx;
  142.                 display: flex;
  143.                 justify-content: center;
  144.                 align-items: center;
  145.                 .issue-item-buy,
  146.                 .issue-item-sell {
  147.                         width: 30%;
  148.                         height: 100rpx;
  149.                         font-size: 28rpx;
  150.                         border-radius: 20rpx;
  151.                         background-color: pink;
  152.                         margin: 40rpx;
  153.                         line-height: 100rpx;
  154.                         text-align: center;
  155.                 }
  156.         }
  157. </style>
复制代码
 下面我给各人先讲讲实现的逻辑,起首逛了一天的博客,有的人用for循环来做,刚开始我也用循环,但是我中心有个凸起的发布按钮,我想做出点击后有弹出层,然后再选择的功能,按照网上他们说的去做都直接跳转了,我这点击发布效果如下图:  没办法我只能我只有会写死,反正反面这个换的也不是太频仍。

我再讲讲代码中必要留意的点吧,起首 如下图的value值我绑定的computed盘算属性中的index,然后methods中的tabBarChange方法呢是点击tabBar切换的每一项,下面我又加个if判断就是用来控制中心发布的谁人图标点击后不跳转

 

 以上设置好后,那就在每一个跳转页配一下代码,作用是用来更store中的changgeTabbarIndex的值,也就是确保页面跳转后,图标选中你所点击的谁人页面,我这里每个页面都设置了一下。代码如下:
  1.                 onShow() {
  2.                         this.$store.commit('changeTabbarIndex', 1)
  3.                 },
复制代码


第三步:安装设置vuex

 起首说为什么要安装vuex,由于通过vuex来实现组件和组件之间数据通报,当你点击差别tabBar来回切换的时间把对应的值存在store中。
安装下令:npm install vuex --save
设置vuex:项目根目次下新建 store/index.js文件,代码如下:

  1. import {
  2.         createStore
  3. } from 'vuex'
  4. const store = createStore({
  5.         //全局状态
  6.         state: {
  7.                 tabbarIndex: 0,
  8.         },
  9.         //同步的方法,commit
  10.         mutations: {
  11.                 changeTabbarIndex(state, index) {
  12.                         state.tabbarIndex = index;
  13.                         console.log('uvexIndex',state.tabbarIndex);
  14.                 }
  15.         },
  16.         //异步的方法 dispatch
  17.         actions: {
  18.         }
  19. })
  20. export default store
复制代码
第四步:设置主入口文件


 到这里就已经完成了,这是本人第一个小步调项目,盼望能给新手们带来点资助,欢迎大佬们前来品评指正。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表