微信小程序中便捷实现自定义底部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下才能选中,就是这个下标配置有问题)
相关推荐
LXXgalaxy3 小时前
微信小程序“记住密码”功能的实现与落地 vue3+ts的细致解析
微信小程序·小程序·notepad++
Greg_Zhong3 小时前
微信小程序中使用【免费商用】字体的下载和初步认识和使用
微信小程序·阿里巴巴、站酷·腾讯云对象存储(cos)
克里斯蒂亚诺更新3 小时前
微信小程序 腾讯地图 点聚合 简单示例
微信小程序·小程序·notepad++
Geek_Vision3 小时前
鸿蒙原生APP接入小程序运行能力:数字园区场景实战复盘
微信小程序·harmonyos
qwfy1 天前
我从瑞幸咖啡小程序里,拆出了一套 22 个组件的开源 UI 库
微信小程序·开源
笨笨狗吞噬者1 天前
小程序包体积分析利器 -- vite-plugin-component-insight
前端·微信小程序·uni-app
Greg_Zhong1 天前
微信小程序中使用canvas中绘制的页面,切换字体的几种实践方式
微信小程序·腾讯云cos·canvas页面切换字体
double_eggm3 天前
微信小程序2
微信小程序·小程序
是江迪呀3 天前
实时看大家都在干嘛?我靠一行监听函数,做了个轻互动小程序
前端·微信小程序