微信小程序自定义tabbar

在这里插入图片描述

app.json

"tabBar": {
    "custom": true, // 开启自定义
 }

项目根目录下新建自定义组件custom-tab-bar

在这里插入图片描述

  • html
<view class="tab-bar" wx:if="{{showBar}}">
  <!-- <cover-view class="tab-bar-border"></cover-view> -->
  <view wx:for="{{listTab}}" wx:key="index" class="tab-bar-item {{item.diyClass}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <image src="{{selected == index ? item.selectedIconPath : item.iconPath}}" class="{{item.diyClass}}"  mode="aspectFit"/> 
    <view wx:if="{{index != 1}}" style="color: {{selected === index ? selectedColor : color}}" class="{{item.diyClass}}">{{item.text}}</view>
  </view>
</view>
  • js
// custom-tab-bar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
 
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    showBar:true,
    selected: 0,
    color: "#999999",
    selectedColor: "#0a9af1",
    listTab: [
      {
        pagePath: "/pages/index/index",
        text: "首页",
        iconPath: "/assets/tabbar/1.png",
        selectedIconPath: "/assets/tabbar/1-1.png"
      },
      {
        pagePath: "/pages/createOrder/index",
        text: "",
        iconPath: "/assets/tabbar/2.png",
        selectedIconPath: "/assets/tabbar/2-2.png",
        diyClass: "diy"
      },
      {
        pagePath: "/pages/my/index",
        text: "我的",
        iconPath: "/assets/tabbar/4.png",
        selectedIconPath: "/assets/tabbar/4-4.png"
      }
    ]
  },
  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path
      wx.switchTab({url}) 
    }
  }
})  
  • css
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  box-shadow: 0px -2px 10px 0px rgba(0,0,0,0.05);
  box-sizing: content-box;
}

.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: auto;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: #fff;
  height: 120rpx;
}

.tab-bar-item.diy {
  margin-top: 0!important;
  position: relative;
  flex: inherit;
  width: 155rpx;
  background: #fff;
}

.tab-bar-item image {
  width: 48rpx;
  height: 48rpx;
  /* overflow: initial; */
}
.tab-bar-item view {
  font-size: 24rpx;
}

.tab-bar-item image.diy {
  position: absolute;
  width: 180rpx;
  height: 180rpx;
  bottom: 2%;
  z-index: 100;
}

.tab-bar-item view.diy {
  margin-top: 90rpx;
  background: rgb(180, 60, 60);
  width: 100%;
  height: 100%;
  padding-top: 58rpx;
  z-index: 99;
}

在所有tabbar页面,手动指定selected 按索引顺序——例如我的页面,首页不需要,下单不显示也不需要

onLoad(options) {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2 //0,1,2 0-首页 1-新建  2-我的
      })
    }
  },

下单页面不需要展示底部菜单;下单页面没有返回按钮,需要加一个手写一个返回按钮

 onLoad() {
    this.getTabBar().setData({
      showBar: false
    });
  },

比input 等原生组件层级更高的只有 cover-view cover-image

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值