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

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

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


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


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();
  }
    })


最后, 效果图:


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



相关文章
|
2月前
|
小程序 开发者
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
本文介绍了如何在Taro项目中实现自定义tabBar。首先,在`app.config.ts`中设置`custom: true`并配置`tabBar`。
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
|
3月前
|
小程序 前端开发
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
86 0
|
3月前
|
JSON 小程序 数据库
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
94 0
|
3月前
|
小程序 API
【微信小程序-原生开发】实用教程07 - Grid 宫格导航,详情页,侧边导航(含自定义页面顶部导航文字)
【微信小程序-原生开发】实用教程07 - Grid 宫格导航,详情页,侧边导航(含自定义页面顶部导航文字)
61 0
|
2月前
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
62 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
2月前
|
小程序 前端开发 JavaScript
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
【避坑宝】是一款企业黑红名单吐槽小程序,旨在帮助打工人群体辨别企业优劣。该平台采用SpringBoot+MybatisPlus+uniapp+uview2等技术栈构建,具备丰富的注释与简洁的代码结构,非常适合实战练习与学习。通过小程序搜索“避坑宝”即可体验。
61 0
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
|
2月前
|
存储 小程序 JavaScript
|
2月前
|
小程序 前端开发 安全
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
44 7
|
3月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的宠物医院微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
61 7

热门文章

最新文章

下一篇
无影云桌面