微信小程序---自定义tabbar

0. 文档位置

自定义tabbarhttps://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

 1. 使用

1.1 随便创建一个文件夹---创建一个commponent

1.2 html

<!--miniprogram/custom-tab-bar/index.wxml-->
<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <!-- 循环列表,获取路由及index -->
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">

    <block wx:if="{{ index === 2 }}" >
      <cover-view class="pub">{{item.text}}</cover-view>
    </block>
    <block wx:else>
      <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
    </block>
  
  </cover-view>
</cover-view>

<!--选中的索引,根据tabbar中js配的菜单位置,自己调整-->
<!-- <tabber selected="{{ 3 }}"></tabber> -->

1.3 css

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.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: 27px;
  height: 27px;
}

.tab-bar-item cover-view {
  font-size: 10px;
}
.pub {
  background-color: #fa3d;
  height: 80rpx;
  width: 80rpx;
  border-radius: 50%;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

1.4 js

var app = getApp();
Component({

  properties: {
    selected: {
      type: Number, // html选中找两个地方,一个data,一个属性 全局就能用
      value: 0
    }
  },
  data: {
    color: "#7A7E83", // 颜色
    selectedColor: "#FF0000", // 被选中颜色
    list: [{
        pagePath: "/pages/index/index",
        iconPath: "/static/tabbar/no.png",
        selectedIconPath: "/static/tabbar/yes.png",
        text: "首页"
      },
      {
        pagePath: "/pages/auction/auction",
        text: "拍卖",
        iconPath: "/static/tabbar/no.png",
        selectedIconPath: "/static/tabbar/yes.png"
      },
      {
        text: "发布"
      },
      {
        pagePath: "/pages/news/news",
        text: "消息",
        iconPath: "/static/tabbar/no.png",
        selectedIconPath: "/static/tabbar/yes.png"
      },
      {
        pagePath: "/pages/home/home",
        iconPath: "/static/tabbar/no.png",
        selectedIconPath: "/static/tabbar/yes.png",
        text: "我的"
      },
    ]
  },
  attached() {},
  methods: {
    switchTab(e) {
      // console.log(e.currentTarget.dataset.path)
      var url = e.currentTarget.dataset.path
      // 根据index判断,发布是渲染的时候是没有url的
      if (url) {
        wx.switchTab({
          url
        })
      } else {
        if (app.globalData.userinfo) {
          wx.navigateTo({
            url: '/pages/release/release',
          })
        } else {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      }


      // this.setData({
      //   selected: data.index
      // })
    }
  }
})

1.5 全局app.json配置

  • 原先的tarbar页面,如果是在app.json中配置了,依旧是无法跳转的,需要在app.json去除,才能正常跳转
  • custom不使用默认的tabbar,使用自定义的,一旦加上,默认的就会失效

  "tabBar": {
    "selectedColor": "#CD5C5C",
    "custom": true,
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/static/tabbar/no.png",
        "selectedIconPath": "/static/tabbar/yes.png"
      },
      {
        "pagePath": "pages/auction/auction",
        "text": "拍卖",
        "iconPath": "/static/tabbar/no.png",
        "selectedIconPath": "/static/tabbar/yes.png"
      },
      {
        "pagePath": "pages/news/news",
        "text": "消息",
        "iconPath": "/static/tabbar/no.png",
        "selectedIconPath": "/static/tabbar/yes.png"
      },
      {
        "pagePath": "pages/home/home",
        "text": "我的",
        "iconPath": "/static/tabbar/no.png",
        "selectedIconPath": "/static/tabbar/yes.png"
      }
    ]
  },

1.6 使用的界面配置(一般会在原tabbar的界面配置)

1.6.1 app.json

{
  "usingComponents": {
    "tabber": "/commponents/tabbar"
  }
}

1.6.2 html(写在最后/最前)  这个页面是我的 索引为2

<!--选中的索引,根据自定义tabbar中js配的菜单位置,自己调整-->
<tabber selected="{{ 0 }}"></tabber>

2. 使用步骤

# 写一个组件
# app.json配置
# 使用
    # 在使用的页面的json中配置
    # 在原先的tabbar的页面上直接使用
    # 导入select(选中变色)
    # 原先的tarbar页面,如果是在app.json中配置了,是无法直接跳转的,需要在app.json去除,才可以正常跳转

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

骑猪去兜风z1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值