当前位置:   article > 正文

【微信小程序】自定义tabbar_微信小程序自定义tabbar

微信小程序自定义tabbar
1、app.json中配置 tabbar
"tabBar": {
    "custom": true,
    "list":[
      {
        "pagePath": "pages/aboutFind/find/find",
        "text": "发现",
        "iconPath": "/public/image/icon_find2.png",
        "selectedIconPath": "/public/image/icon_find1.png"
      },
      {
        "pagePath": "pages/index/index",
        "text": "通行",
        "iconPath": "/public/image/icon_go2.png",
        "selectedIconPath": "/public/image/icon_go1.png"
      },
      {
        "pagePath": "pages/aboutMe/myInfo/myInfo",
        "text": "我的",
        "iconPath": "/public/image/icon_set2.png",
        "selectedIconPath": "/public/image/icon_set1.png"
      }
    ]
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

注意添加: "custom": true

2、新建根目录文件:custom-tab-bar, 并以组件形式新增文件

在这里插入图片描述

注意点:文件夹要在根目录下,名字为custom-tab-bar,并且里面的每个文件名为index

(1)index.wxml

<view class="tabBar">
  <view class="cont">
    <block wx:for="{{list}}" wx:key="index" class="cont-item">
      <view data-path="{{item.pagePath}}" data-index="{{item.pagePath}}" bindtap="switchTab" class="{{item.search?'search':'item'}} {{selected === index ? 'on' : 'off'}}">
        <image src="{{selected === index  ? item.selectedIconPath : item.iconPath}}"></image>
        <view class="txt {{selected === index ? 'selectedColor' : ''}}">{{item.text}}</view>
      </view>
    </block>
  </view>
</view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(2)index.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#fff",
    selectedColor: "#6777FD",
    list: [
      {
        pagePath: "/pages/aboutFind/find/find",
        text: "发现",
        iconPath: "/public/image/icon_find2.png",
        selectedIconPath: "/public/image/icon_find1.png"
      },
      {
        pagePath: "/pages/index/index",
        text: "通行",
        iconPath: "/public/image/icon_go2.png",
        selectedIconPath: "/public/image/icon_go1.png",
        search: true
      },
      {
        pagePath: "/pages/aboutMe/myInfo/myInfo",
        text: "我的",
        iconPath: "/public/image/icon_set2.png",
        selectedIconPath: "/public/image/icon_set1.png"
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({ url })
    }
  }
})
  • 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

(3)index.json

{
  "component": true,
  "usingComponents": {}
}
  • 1
  • 2
  • 3
  • 4

(4)index.wxss

.tabBar {
  z-index: 100;
  width: 100%;
  position: fixed;
  bottom: 0;
  font-size: 28rpx;
  background-color: #fff;
  color: #636363;
  border-radius: 50rpx 50rpx 0px 0px;
}

.cont {
  z-index: 0;
  height: calc(100rpx + env(safe-area-inset-bottom) / 2);
  padding-bottom: calc(env(safe-area-inset-bottom) / 2); 
  padding-bottom: 30rpx;
  display: flex;
  justify-content: space-around;
}

.cont .item {
  font-size: 24rpx;
  position: relative;
  width: 15%;
  text-align: center;
  padding: 0;
  display: block;
  height: auto;
  line-height: 1;
  margin: 0;
  background-color: inherit;
  overflow: initial;
  justify-content: center;
  align-items: center;
  padding-top: 20rpx;
}

.cont .item:first-child {
  right: 45rpx;
}

.cont .item:last-child {
  left: 45rpx;
}

.cont .item image:first-child {
  width: 43rpx !important;
  height: 43rpx !important;
  margin: auto
}

.cont .item image:last-child {
  width: 41rpx !important;
  height: 43rpx !important;
  margin: auto
}

.cont .txt {
  margin-top: 20rpx;
}

.cont .on {
  position: relative;
}

.cont .on:not(:nth-child(2)):before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  width: 100%;
  height: 6rpx;
  background-color: #17C2D8;
  border-radius:120rpx !important;
}

.cont .search {
  position: absolute;
  left: 50%;
  transform: translate(-50%,0);
  top: -50rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.cont .search image {
  width: 100rpx !important;
  height: 100rpx !important;
  z-index: 2;
  border-radius: 100%;
}

.cont .search .txt {
  margin-top: 26rpx;
}

.cont .selectedColor {
  color: #17C2D8;
}
  • 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
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
3、在tab页面中使用

注意:如需实现 tab 选中态,要在当前页面下,通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态

(1)find 自定义tabbar

onShow() {
	if (typeof this.getTabBar === 'function' && this.getTabBar()) {
	  this.getTabBar().setData({
	    selected: 0
	  })
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(2)index 自定义tabbar

onShow() {
	if (typeof this.getTabBar === 'function' && this.getTabBar()) {
	  this.getTabBar().setData({
	    selected: 1
	  })
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(3)myInfo 自定义tabbar

onShow() {
	if (typeof this.getTabBar === 'function' && this.getTabBar()) {
	  this.getTabBar().setData({
	    selected: 2
	  })
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
4、效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

闽ICP备14008679号