小程序二级页面自定义底部tabbar

本文介绍如何在微信小程序的二级页面实现自定义底部tabbar。内容包括在分包下创建自定义tabbar的文件结构,以及在index.wxml、js、json和wxss中的具体实现代码,通过传递idx索引来区分不同页面状态。
部署运行你感兴趣的模型镜像

外面是官方的tabbar  在个人中心进入特定页面 需要自定义一个新的底部tabbar

我这个是分包下的自定义底部tabbar  不分包自定义官方就有 在app.json中

开始-------------------------------------

先新建个文件夹 用来自定义tabar文件

我的文件结构是

 

 自定义底部tabbar 的wxml:

<view class="tab-bar">   
  <block wx:for="{{tabBar}}" wx:key="pagePath">    
  <!-- // data-url 传递参数,可以在navigateDetail方法中的e.currentTarget.dataset.url获取,在tabbar.js代码块中可以看到 -->
    <view class="section_item" bindtap="navigateDetail" data-url="{{item.pagePath}}">  
        <image class="section_image" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>  
        <text class="section_title">{{item.text}}</text>
    </view>
  </block> 
</view>    

js:

Component({
    /**
     * 组件的属性列表
     */
    // 核心功能代码
    properties: {
        idx:{
            type:Number
          }
    },
    /**
     * 组件的初始数据
     */
    data: {
      tabBar: [
        {
          "pagePath": "/pages/tuijianguan/index/index",
          "text": "首页",
          "iconPath": "/assets/images/index/icon_shouye.png",
          "selectedIconPath": "/assets/images/index/icon_shouye_selected.png",
          "selected":false
        },
        {
          "pagePath": "../../user/user",
          "text": "素材",
          "iconPath": "/assets/images/index/icon_sucai.png",
          "selectedIconPath": "/assets/images/index/icon_sucai_selected.png",
          "selected":false
        },
        {
            "pagePath": "../../user/user",
            "text": "学院",
            "iconPath": "/assets/images/index/icon_xueyuan.png",
            "selectedIconPath": "/assets/images/index/icon_xueyuan_selected.png",
            "selected":false
          },
          {
            "pagePath": "/pages/tuijianguan/user/user",
            "text": "我的",
            "iconPath": "/assets/images/index/icon_wode .png",
            "selectedIconPath": "/assets/images/index/icon_wode_selected.png",
            "selected":false
          }
      ]
    },
    observers: {
        // 核心功能代码 
        "idx": function (id) {
          var otabbar = this.data.tabBar;
          otabbar[id]['iconPath'] = otabbar[id]['selectedIconPath']//换当前的icon
          otabbar[id]['selected'] = true;
          console.log(this.data.tabBar)
          this.setData({ tabBar: otabbar});
        }

      },
    /**
     * 组件的方法列表
     */
    methods: {
      navigateDetail: function (e) {
        wx.reLaunch({ // 关闭所有打开过的页面,跳转到相对于的页面
          url: e.currentTarget.dataset.url  // 获取tabbar.wxml中data-url传递的参数
        })
      }
    }
  })
  
  

json:

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

wxss:

.tab-bar {
    width: 100%;
    height: 95rpx;
    position: fixed;
    bottom: 0;
    background: #fff;
    display: flex;
    z-index: 999;
  }
  .section_item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    padding: 5rpx 0 0 0;
  }
  .section_item .section_image {
    width: 55rpx;
    height: 55rpx;
  }
  .section_item .section_title {
    font-size: 20rpx;
    font-weight:300;
  }
  

 

 

 

在使用的文件中代码如下:

index.wxml 这里传了个idx 

<tabBar idx="0"></tabBar>

index.json

{
  "usingComponents": {
    "tabBar": "../components/tabbar/tabbar"
  },
  "navigationStyle": "default"
}

第几页传个idx的索引就行。 

如果不需要改变选中的页面图标  删除多余代码就可以了

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值