微信小程序自定义tabBar

小程序官方提供的方法点击切换会有闪烁,用下面方法不会有这问题,照着粘贴
在这里插入图片描述

  1. app.json
"pages":[
    "pages/classify/classify",
    "pages/scan/scan",
    "pages/my/my",
  ],
  "tabBar": {
    "custom": true,
    "list": [
      {
        "pagePath": "pages/classify/classify"
      },
      {
        "pagePath": "pages/scan/scan"
      },
      {
        "pagePath": "pages/my/my"
      }
    ]
  },

2.创建tabbar组件,根目录下创建一个components文件夹(和pages同级),右键创建一个tabbar文件夹,右键tabbar文件夹->新建Component输入tabbar会出现四个文件
在这里插入图片描述
3.根目录下创建image文件夹,image里放底部图片
在这里插入图片描述

  1. tabbar.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabbar: {
      type: Object,
      value: {
        "backgroundColor": "#ffffff",
        "color": "#999999",
        "selectedColor": "#ed6c9e",
        "list": [
          {
            "pagePath": "/pages/classify/classify",
            "iconPath": "/image/classify.png",
            "selectedIconPath": "/image/classify_select.png",
            "text": "分类"
          },
          {
            "pagePath": "/pages/scan/scan",
            "iconPath": "/image/scan.jpg",
            "selectedIconPath": "/image/scan_select.jpg",
            "isSpecial": true,
            "text": ""
          },
          {
            "pagePath": "/pages/my/my",
            "iconPath": "/image/my.png",
            "selectedIconPath": "/image/my_select.png",
            "text": "我的"
          }
        ]
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    goPage: function (event) {
      var url = event.currentTarget.dataset.id;
      wx.switchTab({
        url: url,
      })
    }
  },
  lifetimes : {
    attached: function() {
      var tabbar = this.properties.tabbar;
      var currentPages = getCurrentPages();
      var currentPage = currentPages[currentPages.length - 1];
      var pagePath = currentPage.route;
      (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
      for (var i in tabbar.list) {
        tabbar.list[i].selected = false;
        (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
      }
      this.setData({
        tabbar: tabbar
       });
      var detail = {"tabbar":tabbar} 
      var option = {}
      this.triggerEvent("customevent",detail,option);
    }
  }
})

5.tabbar.json ,按照第2步创建会自动出现

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

6.tabbar.wxml

<view class="tabbar_box" style="background-color:{{tabbar.backgroundColor}}">
  <block wx:for="{{tabbar.list}}" wx:key="item.pagePath">
    <view wx:if="{{item.isSpecial == true}}" class="tabbar_nav" hover-class="none" bindtap="goPage" data-id="{{item.pagePath}}" style="color:{{tabbar.selectedColor}}">
        <image class="tabbar_icon_special" src="{{item.selected ? item.selectedIconPath : item.iconPath}}" style="box-shadow:{{item.selected ? '0 0 10rpx #ed6c9e':'none'}}"></image>
    </view>  
    <view wx:else class="tabbar_nav" hover-class="none" bindtap="goPage" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" data-id="{{item.pagePath}}">
      <image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
      <text>{{item.text}}</text>
    </view>
  </block>
</view> 

7.tabbar.wxss

.tabbar_box{
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 999;
    width: 100%;
    height: 80rpx;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
}
.tabbar_nav{
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 20rpx;
    height: 100%;
    position: relative;
}
.tabbar_icon{
    width: 38rpx;
    height: 34rpx;
}
.tabbar_icon_special{
  position: absolute;
  top: -24rpx;
    width: 72rpx;
    height: 70rpx;
}

9.使用组建的页面classify.json ,另外两个页面一样

{
  "usingComponents": {
    "tabbar": "/components/tabbar/tabbar"
  },
  "navigationBarTitleText": "分类"
}

10.使用组建的页面classify.wxml ,另外两个页面一样

<tabbar bindcustomevent="onCustomEventEvent"></tabbar>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值