微信小程序中便捷实现自定义底部tab栏

前言:很多东西不记录下,过段时间不用就容易忘...

一、实现步骤:
  1. 在根目录按规范创建底部tab的页面,
  2. 在app.json中配置,告诉项目使用的tab配置,
  3. 在tab指向的页面中,配置tab排列的顺序,通过onshow中配置加载tab的排列顺序,
二、完整代码:
  1. 根目录创建的自定义tab的代码

    bash 复制代码
    Component({
      data: {
        activeIndex: 0,
        tabList: [
    			{
            pagePath: 'pages/login/login',
            text: '首页',
            normalIcon: '../image/img_03.png',
            selectedIcon: '../image/img_04.png'
          },
          {
            pagePath: 'pages/wx-logo/wx-logo', 
            text: 'logo绘制',
            normalIcon: '../image/img_01.png',
            selectedIcon: '../image/img_02.png'
          },
         
        ]
      },
      methods: {
        switchTab(e) {
          const { index, path } = e.currentTarget.dataset;
          if (index === this.data.activeIndex) return;
    
          this.setData({ activeIndex: index });
          wx.switchTab({ url: `/${path}` }); 
        }
      }
    });
    bash 复制代码
    <view class="tab-bar">
      <view 
        wx:for="{{tabList}}" 
        wx:key="index" 
        class="tab-item {{activeIndex === index ? 'active' : ''}}"
        bindtap="switchTab"
        data-index="{{index}}"
        data-path="{{item.pagePath}}"
      >
        <!-- 图标区域:替换为你的图片路径 -->
        <image 
          src="{{activeIndex === index ? item.selectedIcon : item.normalIcon}}" 
          class="tab-icon"
        />
        <!-- 文字区域 -->
        <text class="tab-text">{{item.text}}</text>
      </view>
    </view>
    bash 复制代码
    /* TabBar 容器:固定底部,适配安全区域 */
    .tab-bar {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      height: 124rpx;
      background: #ffffff;
      display: flex;
      border-top: 1px solid #e6e6e6;
      padding-bottom: env(safe-area-inset-bottom); /* 适配 iPhone 底部安全区域 */
      z-index: 999;
    }
    
    /* 单个 Tab 项:平分宽度,居中布局 */
    .tab-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: all 0.2s ease; /* 平滑过渡动画 */
    }
    
    /* 图标样式:默认尺寸 24px,选中 28px(主流放大比例) */
    .tab-icon {
      width: 48rpx;
      height: 48rpx;
      margin-bottom: 2px;
      transition: all 0.2s ease;
    }
    .tab-item.active .tab-icon {
      width: 28px;
      height: 28px;
    }
    
    /* 文字样式:默认 12px #999,选中 14px 主题色(参考微信绿) */
    .tab-text {
      font-size: 20rpx;
      /* color: #999999; */
    	color: #b2aaa2;
      transition: all 0.2s ease;
    }
    .tab-item.active .tab-text {
      font-size: 24rpx;
      color: #ffca28; /* 选中颜色,可自行修改 */
      font-weight: 500; /* 选中时微加粗,增强视觉效果 */
    }

    json中官方说需要配置,我发现配不配都不影响,看个人

    bash 复制代码
    {
      "component": true
    }
  2. app.json中的配置

    bash 复制代码
    	"tabBar": {
        "custom": true, 
        "list": [
          { "pagePath": "pages/login/login", "text": "首页" },
          { "pagePath": "pages/wx-logo/wx-logo", "text": "logo绘制" }
        ]
      },
  3. 底部tab配置跳转的页面中的配置

    bash 复制代码
      onShow() {
    		 // 安全判断,防止 getTabBar 不存在时报错
    		 if (typeof this.getTabBar === 'function' && this.getTabBar()) {
          this.getTabBar().setData({
            activeIndex: 0 // 底部tab中的第1个显示
          })
        }
      },
三、总结:初次自定义tab的几个误区
  1. 直接给自定义的tab文件,随便取名和随便放在其他文件夹内,最后就是无效果,
  2. 在app.json中配置自定义tab的对应页面,以为底部tab的渲染顺序遵从这个数组内的排列顺序?
  3. 其实是遵循在具体页面路径中配置的activeIndex的值的大小,进行排列的,(有时候配置完,需要点击2下才能选中,就是这个下标配置有问题)
相关推荐
好赞科技1 天前
2026年最佳健身小程序推荐榜单,帮你解锁智能运动新体验
大数据·微信小程序
好赞科技1 天前
026年五大汽车保养预约小程序推荐榜单,让养车更轻松省心
大数据·微信小程序
azhou的代码园1 天前
基于微信小程序的图片识别科普系统的设计与实现
vue.js·spring boot·微信小程序·小程序·毕业设计·科普·图片识别
好赞科技2 天前
深度测评2026年最佳GEO流量精准获客工具排行榜,解锁你的营销新高度
大数据·微信小程序
深邃的眼2 天前
微信小程序从 0-1:从本地开发到部署服务器上线整体流程保姆式教学
阿里云·微信小程序·个人开发
喜欢南方姑娘2 天前
微信小程序热更新-用户打开小程序时检测版本自动更新
微信小程序·小程序·notepad++
一叶星殇2 天前
高颜值微信小程序 UI 组件库大盘点,助你轻松开发!
微信小程序·小程序
计算机专业码农一枚2 天前
微信小程序 uniapp+vue高校社团管理
vue.js·微信小程序·uni-app
Raytheon_code2 天前
从零到一:我用微信小程序做了一款串珠DIY定制工具
css·微信小程序·html5·ai编程
yzx9910132 天前
从零开始写一个微信小程序:完整代码实战指南(入门篇)
微信小程序·小程序·notepad++