当前位置:   article > 正文

小程序自定义底部导航。。。干货。。。custom-tab-bar得使用

custom-tab-bar

1、首先 app.json文件 找到 tabBar 添加 “custom”: true 开启自定义tabBar
在这里插入图片描述
2、根目录下新建custom-tab-bar目录 新建index(Component组件)**必须是index命名
在这里插入图片描述
2.1 wxml 文件

<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected === index ? selectedColor : color}};font-size:28rpx;margin-top:6rpx;">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.2 wxss文件

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding-top: 20rpx;
  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: 2rpx;
  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: 54rpx;
  height: 54rpx;
  margin-bottom: 10rpx;
}

.tab-bar-item cover-view {
  font-size: 20rpx;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

2.3 js文件


Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#cf000e",
    "list": [
      {
        "pagePath": "/pages/index/index",
        "text": "首页",
        "iconPath": "/img/home.png",
        "selectedIconPath": "/img/home_on.png"
      },
      {
        "pagePath": "/pages/category/category",
        "text": "分类",
        "iconPath": "/img/sort.png",
        "selectedIconPath": "/img/sort_on.png"
      },
      {
        "pagePath": "/pages/shoppingcart/tabBarCart",
        "text": "购物车",
        "iconPath": "/img/cart.png",
        "selectedIconPath": "/img/cart_on.png"
      },
      {
        "pagePath": "/pages/mine/mine",
        "text": "我的",
        "iconPath": "/img/my.png",
        "selectedIconPath": "/img/my_on.png"
      }
    ]
  },
  onshow() {
    console.log('onshow.....')
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      console.log(data)
      const url = data.path
      wx.switchTab({ url })
      console.log(e)
      this.setData({
        selected: data.index
      })
    }
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.4 json文件

{
  "component": true
}
  • 1
  • 2
  • 3

3.使用~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/704601
推荐阅读
相关标签