你会微信小程序自定义底部导航栏吗?

简介: 你会微信小程序自定义底部导航栏吗?

一、 在目录下创建文件如下图:


网络异常,图片无法展示
|


tabbar.js:


// pages/tabbar/tabbar.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
      tabbar: {
        type: Object,
        value: {
           "backgroundColor": "#ffffff",
           "color": "#333",
           "selectedColor": "#fca89d",
           "borderStyle": "white",
            "list": [{
                 "pagePath": "/pages/index/index",
                 "text": "首页",
                 "iconPath": "http://192.168.2.61/wap/rm/images/in.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/ined.png"
               },
               {
                 "pagePath": "/pages/mry/mry",
                 "text": "美容院",
                 "iconPath": "http://192.168.2.61/wap/rm/images/mei.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/meied.png"
               },
               {
                 "pagePath": "/pages/shop/shop",
                 "text": "商城",
                 "iconPath": "http://192.168.2.61/wap/rm/images/shop.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/shoped.png"
               },
               {
                 "pagePath": "/pages/mine/mine",
                 "text": "我的",
                 "iconPath": "http://192.168.2.61/wap/rm/images/min.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/mined.png"
               }
             ],
        }
      }
  },
  /**
   * 组件的初始数据
   */
  data: {
  },
  /**
   * 组件的方法列表
   */
  methods: {
  }
})


tabbar.json:


{
  "component": true,
  "usingComponents": {
  }
}


tabbar.wxml:


<!--pages/tabbar/tabbar.wxml-->
<view class="tabbar_box {{isIphoneX?'iphoneX-height':''}}" style="background-color:{{tabbar.backgroundColor}}">
    <block wx:for="{{tabbar.list}}" wx:key="{{item.pagePath}}">
        <navigator wx:if="{{item.isSpecial}}" class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{tabbar.selectedColor}}" open-type="navigate">
            <view class='special-wrapper'>
                <image class="tabbar_icon" src="{{item.iconPath}}"></image>
            </view>
            <image class='special-text-wrapper'></image>
            <text class="txt">{{item.text}}</text>
        </navigator>
        <navigator wx:else class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="switchTab">
            <image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
            <text class="txt">{{item.text}}</text>
        </navigator>
    </block>
</view>


tabbar.wxss:


/* pages/tabbar/tabbar.wxss */
.tabbar_box { display: flex; flex-direction: row; justify-content: space-around; position: fixed; bottom: 0; left: 0; z-index: 999; width: 100%; height: 98rpx; box-shadow: 0 0 2px rgba(0, 0, 0, 0.1); }
.tabbar_box.iphoneX-height { padding-bottom: 66rpx; }
.tabbar_nav { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 20rpx; height: 100%; position: relative; }
.tabbar_icon { width: 40rpx; height: 40rpx; margin:7rpx 0; }
.special-wrapper .tabbar_icon { width: 84rpx; height: 84rpx; }
.special-text-wrapper { width: 45rpx; height: 45rpx; }
.txt{font-size: 26rpx;}


二、然后需要在以下全局文件做下修改


app.js:


//app.js
App({
   onLaunch: function () {
     // 隐藏自带的tabbar
     wx.hideTabBar();
   },
       editTabbar: function () {
           let tabbar = this.globalData.tabBar;
           let currentPages = getCurrentPages();
           let _this = currentPages[currentPages.length - 1];
           let pagePath = _this.route;
           (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
           for (let i in tabbar.list) {
             tabbar.list[i].selected = false;
             (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
           }
           _this.setData({
             tabbar: tabbar
           });
         },
         globalData: {
           systemInfo: null, //客户端设备信息
           userInfo: null,
           tabBar: {
             "backgroundColor": "#ffffff",
             "color": "#333",
             "selectedColor": "#fca89d",
             "borderStyle": "white",
             "list": [{
                 "pagePath": "/pages/index/index",
                 "text": "首页",
                 "iconPath": "http://192.168.2.61/wap/rm/images/in.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/ined.png"
               },
               {
                 "pagePath": "/pages/mry/mry",
                 "text": "美容院",
                 "iconPath": "http://192.168.2.61/wap/rm/images/mei.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/meied.png"
               },
               {
                 "pagePath": "/pages/shop/shop",
                 "text": "商城",
                 "iconPath": "http://192.168.2.61/wap/rm/images/shop.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/shoped.png"
               },
               {
                 "pagePath": "/pages/mine/mine",
                 "text": "我的",
                 "iconPath": "http://192.168.2.61/wap/rm/images/min.png",
                 "selectedIconPath": "http://192.168.2.61/wap/rm/images/mined.png"
               }
             ],
           }
         }
})


app.json:


"tabBar": {
    "backgroundColor": "#ffffff",
    "color": "#333",
    "selectedColor": "#fca89d",
    "borderStyle": "white",
    "list": [
      {
        "pagePath": "pages/index/index"
      },
      {
        "pagePath": "pages/mry/mry"
      },
      {
        "pagePath": "pages/shop/shop"
      },
      {
        "pagePath": "pages/mine/mine"
      }
    ]
  }


三 、需要引入的tabbar,如index.wxml


index.wxml:


<tabbar tabbar="{{tabbar}}"></tabbar>


index.json:


{
    "usingComponents": {
      "tabbar": "/component/tabbar/tabbar"
    }
}


index.js:


const app = getApp();
Page({
  data: {
    tabbar: {},
    },
  onLoad: function () {
     app.editTabbar();
  }
    })


最后, 效果图:


网络异常,图片无法展示
|



相关文章
|
人工智能 小程序 API
【一步步开发AI运动小程序】十三、自定义一个运动分析器,实现计时计数02
本文介绍如何利用“云智AI运动识别小程序插件”开发AI运动小程序,详细解析了俯卧撑动作的检测规则构建与执行流程,涵盖卧撑和撑卧两个姿态的识别规则,以及如何通过继承`sports.SportBase`类实现运动分析器,适用于小程序开发者。
|
人工智能 小程序 API
【一步步开发AI运动小程序】十二、自定义一个运动分析器,实现计时计数01
随着AI技术的发展,AI运动APP如雨后春笋般涌现,如“乐动力”、“天天跳绳”等,推动了云上运动会、线上健身等热潮。本文将指导你从零开始开发一个AI运动小程序,利用“云智AI运动识别小程序插件”,介绍运动识别原理、计量方式及运动分析器基类的使用,帮助你在小程序中实现运动计时和计数功能。下篇将继续探讨运动姿态检测规则的编写。
|
小程序 开发者
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
本文介绍了如何在Taro项目中实现自定义tabBar。首先,在`app.config.ts`中设置`custom: true`并配置`tabBar`。
1670 1
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
|
小程序
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合(更新版)
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合(更新版)
3384 1
|
小程序 搜索推荐 API
微信小程序:自定义关注公众号组件样式
尽管关注公众号组件的样式固定且不可修改,但产品经理的需求却需要个性化的定制。在这种情况下,我们需要寻找解决方案,以满足这些特殊需求,尽管这可能有点棘手。
1589 0
微信小程序:自定义关注公众号组件样式
|
JSON 小程序 JavaScript
uni-app开发微信小程序的报错[渲染层错误]排查及解决
uni-app开发微信小程序的报错[渲染层错误]排查及解决
4925 7
|
小程序 前端开发 测试技术
微信小程序的开发完整流程是什么?
微信小程序的开发完整流程是什么?
2724 7
|
小程序 JavaScript 前端开发
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
5360 1
|
人工智能 自然语言处理 小程序
技术小白如何利用DeepSeek半小时开发微信小程序?
通过通义灵码的“AI程序员”功能,即使没有编程基础也能轻松创建小程序或网页。借助DeepSeek V3和R1满血版模型,用户只需用自然语言描述需求,就能自动生成代码并优化程序。例如,一个文科生仅通过描述需求就成功开发了一款记录日常活动的微信小程序。此外,通义灵码还提供智能问答模式,帮助用户解决开发中的各种问题,极大简化了开发流程,让普通人的开发体验更加顺畅。
4233 11
技术小白如何利用DeepSeek半小时开发微信小程序?
|
小程序 Java 关系型数据库
weixin163基于微信小程序的校园二手交易平台系统设计与开发ssm(文档+源码)_kaic
本文介绍了一款基于微信小程序的校园二手物品交易平台的开发与实现。该平台采用Java语言开发服务端,使用MySQL数据库进行数据存储,前端以微信小程序为载体,支持管理员和学生两种角色操作。管理员可管理用户、商品分类及信息、交易记录等,而学生则能注册登录、发布购买商品、参与交流论坛等。系统设计注重交互性和安全性,通过SSM框架优化开发流程,确保高效稳定运行,满足用户便捷交易的需求,推动校园资源共享与循环利用。