小程序原生Tabbar组件菜单配置

Tabbar组件必须放在小程序根目录下

文件夹名:custom-tab-bar

文件名都是:index

index.js

Component({

data: {

selected: 0,

color: "#333",

selectedColor: "#D70010",

list: [{

  pagePath: "/pages/index/index",

  iconPath: "/images/tabbar/shouye.png",

  selectedIconPath: "/images/tabbar/shouye_active.png",

  text: "主页"

}, {

  pagePath: "/pages/categoryList/index",

  iconPath: "/images/tabbar/fenlei.png",

  selectedIconPath: "/images/tabbar/fenlei_active.png",

  text: "分类"

}, {

  pagePath: "/pages/unioncart/index",

  iconPath: "/images/tabbar/gouwuche.png",

  selectedIconPath: "/images/tabbar/gouwuche_active.png",

  text: "购物车"

},{

  iconPath: "/images/tabbar/kefu.png",

  selectedIconPath: "/images/tabbar/kefu_active.png",

  text: "客服"

}, {

  pagePath: "/pages/my/index",

  iconPath: "/images/tabbar/wode.png",

  selectedIconPath: "/images/tabbar/wode_active.png",

  text: "我的"

}]

},

attached() {

},

methods: {

switchTab(e) {

  const data = e.currentTarget.dataset

  const url = data.path

  wx.switchTab({url})

  this.setData({

    selected: data.index

  })

}

}

})

json

{

“component”: true

}

wxml

<block wx:if="{{index!=3}}">

<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>

<cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  <button class="tab-bar-item cs_button" open-type='contact'>

  <cover-image style="text-align: center;margin-left: 6rpx;" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>

  <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>

</button> 

wxss

.tab-bar {

position: fixed;

bottom: 0;

left: 0;

right: 0;

height: 100rpx;

background: white;

display: flex;

border-top: 1px solid #f1f1f1;

padding-bottom: env(safe-area-inset-bottom);

}

.cs_button{

background: #fff;

line-height: 38rpx;

}

.cs_button:after {

border: 0px;

}

.tab-bar-border {

/* background-color: rgba(0, 0, 0, 0.33); */

position: absolute;

left: 0;

top: 0;

width: 100%;

height: 1px;

transform: scaleY(0.5);

}

.tab-bar-item {

flex: 1;

text-align: center;

display: flex;

justify-content: center;

align-items: center;

flex-direction: column;

}

.tab-bar-item cover-image {

width: 48rpx;

height: 48rpx;

}

.tab-bar-item cover-view {

font-size: 30rpx;

}

app.json配置

“tabBar”: {

"custom": true,

"color": "#7A7E83",

"selectedColor": "#3cc51f",

"borderStyle": "black",

"backgroundColor": "#ffffff",

"list": [

  {

    "pagePath": "pages/index/index",

    "iconPath": "images/tabbar/shouye.png",

    "selectedIconPath": "images/tabbar/shouye_active.png",

    "text": "主页"

  }, {

    "pagePath": "pages/categoryList/index",

    "iconPath": "images/tabbar/fenlei.png",

    "selectedIconPath": "images/tabbar/fenlei_active.png",

    "text": "分类"

  }, {

    "pagePath": "pages/unioncart/index",

    "iconPath": "images/tabbar/gouwuche.png",

    "selectedIconPath": "images/tabbar/gouwuche_active.png",

    "text": "购物车"

  }, {

    "pagePath": "pages/my/index",

    "iconPath": "images/tabbar/wode.png",

    "selectedIconPath": "images/tabbar/wode_active.png",

    "text": "我的"

  }

]

},

在页面引入菜单组件

在js中增加

onShow: function() {

if (typeof this.getTabBar === 'function' &&

    this.getTabBar()) {

    this.getTabBar().setData({

      selected: 1

    })

  }

},

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值