微信小程序——自定义tabbar组件

项目github地址:https://github.com/zhongjunyao/comp-tabbar.git

核心代码:

app.js

//app.js
App({
  onLaunch: function () {
  },
  globalData: {
    userInfo: null,
    tabbar: {
      color: "#A5A5A5",
      selectedColor: "#FFDC1D",
      backgroundColor: "#000000",
      borderStyle: "#000000",
      list: [
        {
          pagePath: "pages/index/index",
          iconPath: "/images/index.png",
          selectedIconPath: "/images/index_select.png",
          text: "首页"
        },
        {
          pagePath: "pages/log/log",
          iconPath: "/images/shoucang.png",
          selectedIconPath: "/images/shoucang_select.png",
          text: "日志"
        },
        {
          pagePath: "pages/my/my",
          iconPath: "/images/huiyuan.png",
          selectedIconPath: "/images/huiyuan_select.png",
          text: "我的"
        }
      ]
    }
  }
})

组件:tabbar

// tabbar.wxml

<view class="comp-tabbar" style="border-color:{{tabbar.borderStyle}};background-color:{{tabbar.backgroundColor}}">
  <view bindtap="redirectTo" data-taburl="{{item.pagePath}}" wx:for="{{tabbar.list}}" wx:key="index" class="tabbarItem" style="color:{{curRoute==item.pagePath?tabbar.selectedColor:tabbar.color}}">
    <image class="icon" src="{{curRoute==item.pagePath?item.selectedIconPath:item.iconPath}}" mode="aspectFit"></image>
    <view>{{item.text}}</view>
  </view>
</view>

// tabbar.wxss
.comp-tabbar{
  position:fixed;bottom:0;
  display:flex;
  align-items:center;
  width:100%;
  height:94rpx;
  box-sizing:border-box;
  border-top:1rpx solid;
  text-align:center;
  font-size:22rpx;
}
.comp-tabbar .tabbarItem{
  flex:1;height:inherit;
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
}
.comp-tabbar .tabbarItem .icon{
  width:50rpx;
  height:50rpx;
}

// tabbar.json
{
  "component":true
}

// tabbar.js
Component({

  data:{
    tabbar:{},
    curRoute:''
  },
  attached(){
    this.data.tabbar = getApp().globalData.tabbar;
    let pages = getCurrentPages();
    this.data.curRoute = pages[pages.length-1].route;
    this.setData(this.data)
  },
  methods:{
    redirectTo(e){
      let taburl = e.currentTarget.dataset.taburl;
      if(taburl == this.data.curRoute) return
      wx.redirectTo({
        url:"/"+taburl
      })
    },
  }
})

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值