当前位置:   article > 正文

uniapp 自定义tabBar导航栏_uniapp自定义tabbar

uniapp自定义tabbar

1.在App.vue文件里把原生的tabBar导航栏,隐藏掉

  1. //App.vue
  2. onLaunch: function() {
  3. //隐藏tabBar
  4. uni.hideTabBar()
  5. uni.removeStorageSync('selectedIndex');
  6. },

2.在components文件下,创建一个uni-tab-bar.vue文件

  1. <template>
  2. <!-- <view class="tabBars">
  3. <view class="tabs" v-for="(item,index) in tabList" :key="index" @tap="switchTab(index)">
  4. <image class="imgs" :src="$jointImage(item.selectedIconPath)" v-if="item.state"></image>
  5. <image class="imgs" :src="$jointImage(item.iconPath)" v-else></image>
  6. <view class="text" :style="{color:isActive(item.pagePath) ? ' #76bd21' : '' }">{{item.text}}</view>
  7. </view>
  8. </view> -->
  9. <uni-transition mode-class="fade" :duration="200" :show="true">
  10. <view>
  11. <view class="tab-content">
  12. <slot />
  13. </view>
  14. <view class="tabbar">
  15. <view class="navigator">
  16. <view ref='warpper' class="warpper">
  17. <view ref="navItem" class="navigator-item" v-for="(item,index) in tabList" :key="item.pagePath"
  18. @click="switchTab(item,index)" :data-index='index'>
  19. <image class="icon" :src="$jointImage(item.iconPath)" v-if="selectedIndex !== index">
  20. </image>
  21. <image class="icon" :src="$jointImage(item.selectedIconPath)" v-else></image>
  22. <text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </uni-transition>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. selectedIndex: uni.getStorageSync('selectedIndex') || 0,
  35. configuration: {},
  36. tabList: [],
  37. };
  38. },
  39. mounted() {
  40. this.alliconSet()
  41. },
  42. methods: {
  43. alliconSet() {
  44. uni.$http.post('index/iconSet', {}).then(res => {
  45. this.configuration = res.data.data
  46. this.tabList = [{
  47. pagePath: "/pages/index/index",
  48. text: "首页",
  49. state: true,
  50. iconPath: this.configuration.tabbar1_nocheck,
  51. selectedIconPath: this.configuration.tabbar1_check
  52. },
  53. {
  54. pagePath: "/pages/menu/menu",
  55. text: "点单",
  56. state: false,
  57. iconPath: this.configuration.tabbar2_nocheck,
  58. selectedIconPath: this.configuration.tabbar2_check
  59. },
  60. {
  61. pagePath: "/pages/order_two/order_two",
  62. text: "订单",
  63. state: false,
  64. iconPath: this.configuration.tabbar3_nocheck,
  65. selectedIconPath: this.configuration.tabbar3_check
  66. },
  67. {
  68. pagePath: "/pages/my/my",
  69. text: "我的",
  70. state: false,
  71. iconPath: this.configuration.tabbar4_nocheck,
  72. selectedIconPath: this.configuration.tabbar4_check
  73. },
  74. ]
  75. })
  76. },
  77. switchTab(items, indexs) {
  78. uni.switchTab({
  79. url: `${this.tabList[indexs].pagePath}`
  80. });
  81. this.tabList.forEach((v, i) => {
  82. if (items.pagePath === v.pagePath) {
  83. uni.setStorageSync('selectedIndex', indexs);
  84. }
  85. })
  86. },
  87. },
  88. };
  89. </script>
  90. <style lang="scss" scoped>
  91. .tabbar {
  92. position: fixed;
  93. bottom: 0;
  94. left: 0;
  95. width: 100%;
  96. height: 140rpx;
  97. z-index: 999;
  98. background: #F5F5F5;
  99. border-top: 2rpx solid #eee;
  100. }
  101. .navigator {
  102. width: 85%;
  103. margin: 0 auto;
  104. padding: 20rpx;
  105. overflow: hidden;
  106. }
  107. .warpper {
  108. display: flex;
  109. justify-content: space-between;
  110. width: auto;
  111. transition-timing-function: ease-out;
  112. }
  113. .navigator-item {
  114. display: flex;
  115. align-items: center;
  116. flex-direction: column;
  117. width: 50rpx;
  118. height: 100%;
  119. }
  120. .item-text {
  121. margin-top: 6rpx;
  122. color: #777E86;
  123. font-size: 24rpx;
  124. }
  125. .text-active {
  126. color: #76bd21 !important;
  127. }
  128. .icon {
  129. width: 24px;
  130. height: 24px;
  131. }
  132. </style>

3.在main.js引入组件

  1. import UniTabBar from '@/components/uni-tab-bar.vue'
  2. Vue.component('UniTabBar', UniTabBar)

4.在需要的页面,使用组件

  1. //index.vue
  2. <template>
  3. <view>
  4. <UniTabBar />
  5. </view>
  6. </template>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/796689
推荐阅读
相关标签
  

闽ICP备14008679号