欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现微信小程序底部tab栏自定义开发-封装tabBar组件

程序员文章站 2024-03-21 11:31:04
...

小程序中因为购物带需要小圆点或者是加购件数的需求,需要自定义开发来满足。

一.首先自定义一个组件tabbar

1.tabbar的wxml

<view class='tabbar'  wx:if="{{activeIdx!=0||!landingPage}}" style='padding-bottom:{{tabBarHeight}}rpx'>
  <view wx:if='{{_auth < item.auth}}' class='tabbar-item {{activeIdx === index ? "active" : ""}}' wx:for='{{tabbarList}}' wx:key='{{item.pagePath}}' bindtap='handleItemTap' data-path='{{item.pagePath}}' data-idx='{{index}}'>
    <view wx:if="{{index!==2}}">
      <view class='tabbar-item-icon'>
        <image class="tabbar-item-icon-img" src='{{activeIdx === index ? item.selectedIconPath : item.iconPath}}'>
        </image>
      </view>
      <view class='tabbar-item-text'>{{item.text}}</view>
    </view>
    <view wx:else>
      <view class='tabbar-item-icon posrelative'>
        <view wx:if="{{cartNum}}" class="noStyle"></view>
        <image class="tabbar-item-icon-img" src='{{activeIdx === index ? item.selectedIconPath : item.iconPath}}'>
        </image>
      </view>
      <view class='tabbar-item-text'>{{item.text}}</view>
    </view>
  </view>
</view>

2.tabbar的wxss

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100vw;
  border-top: 0.5px solid #d5d5d5;
  display: flex;
  font-size: 0;
  background-color: #000;
}

.tabbar-item {
  flex: 1;
  text-align: center;
  overflow: hidden;
  box-sizing: border-box;
  padding: 17rpx 10rpx 17rpx;
  color: #616365;
}

.tabbar-item-icon {
  margin-bottom: 13rpx;
}

.tabbar-item-icon .tabbar-item-icon-img {
  width: 44rpx;
  height: 44rpx;
  position: relative;
}
.cover-view-tabbar-item-icon-img{
  left: 50%;
  transform: translateX(-50%);
}
.tabbar-item-text {
  font-size: 20rpx;
  letter-spacing: 2rpx;
  line-height: 21rpx;
}

.tabbar-item.active {
  color: #762322;
}

.carNum {
  position: absolute;
  width: 44rpx;
  height: 44rpx;
  border-radius: 50%;
  font-size: 16rpx;
  text-indent: -4rpx;/*让字体看起来居中*/
  top: 0;
  left: 0;
  line-height: 60rpx;
  text-align: center;
}
.noStyle{
  position: absolute;
  z-index: 10000;
  display: inline-block;
  width: 17rpx;
  background-color: #762322;
  color: #fff;
  border-radius:50%;
  height: 17rpx;
  line-height: 17rpx;
  margin-left: 32rpx;
  font-size: 17rpx;
  text-align: center
}

3.tabbar的js


// components/tabbar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activeIdx: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabbarList: [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/icon/index.png",
        "selectedIconPath": "/icon/index-act.png",
        "auth": 1
      },
      {
        "pagePath": "pages/classifyIndex/classifyIndex",
        "text": "分类",
        "iconPath": "/icon/classify.png",
        "selectedIconPath": "/icon/classify-act.png",
        "auth":1
      },
      // {
      //   "pagePath": "pages/search/search",
      //   "text": "搜索",
      //   "iconPath": "/icon/search.png",
      //   "selectedIconPath": "/icon/search-act.png",
      //   "auth": 1
      // },
      {
        "pagePath": "pages/cart/cart",
        "text": "购物袋",
        "iconPath": "/icon/cart.png",
        "selectedIconPath": "/icon/cart-act.png",
        "auth":1
      },
      {
        "pagePath": "pages/mine/mine",
        "text": "我的",
        "iconPath": "/icon/mine.png",
        "selectedIconPath": "/icon/mine-act.png",
        "auth": 1
      }
    ],
    _auth: 0,
    tabBarHeight: getApp().globalData.tabBarHeight,
    cartNum:false
  },
  lifetimes: {
    attached: function () {
      getApp().getconfig(this);
    }
  },
  ready:function(){
    console.log(this.data.cartNum)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getconfig(e){
      this.setData({
        'tabbarList[2].auth': e.cartStatus
      })
    },
    handleItemTap(e) {
      const {
        idx,
        path
      } = e.currentTarget.dataset
      if (idx === this.data.activeIdx) return
      wx.switchTab({
        url: `/${path}`,
      })
    },
    showIcon(){
      this.setData({
        cartNum: wx.getStorageSync("currentAddCart").length == 0 ? false : true
      })
      console.log('cartNum', this.data.cartNum)
    }
  },
  //显示购物袋中的showIcon
  pageLifetimes: {
    show: function () {
      this.showIcon();
    }
  }
})

二. 引用tab栏

1.app.json中需要保留tab栏的配置,tabBar在首页,购物袋,我的,分类页面都需要引用,所以我对组件的引用直接配置在app.json中了

实现微信小程序底部tab栏自定义开发-封装tabBar组件

2.在你需要的页面中引入tabbar组件,比如首页增加箭头指的那句即可

实现微信小程序底部tab栏自定义开发-封装tabBar组件

相关标签: 小程序