当前位置:   article > 正文

uniapp小程序实现自定义tabbar_uniapp 小程序自定义tabbar

uniapp 小程序自定义tabbar

先在项目公共组件中创建文件

组件代码

  1. <template>
  2. <view class="tab_bar">
  3. <view class="tabbarBox">
  4. <view class="handleBox" v-for="(item, index) in tabBarList" :key="index" @click="goPages(item.pagePath, index)">
  5. <view class="menuBox">
  6. <image class="img" :src="selectIndex === index ? item.selectedIconPath : item.iconPath"></image>
  7. <text class="menuBoxText" :class="index === selectIndex ? 'TextColor' : 'Text'">{{ item.text }}</text>
  8. </view>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script lang="ts" setup>
  14. import { ref } from 'vue';
  15. const props = defineProps({
  16. selectIndex: {
  17. type: Number,
  18. }
  19. })
  20. // iconPath 没选择图标
  21. // selectedIconPath 选中时的图标
  22. const tabBarList = [
  23. {
  24. pagePath: "/pages/home/index",
  25. iconPath: "/static/icon_main_tab_home.png",
  26. selectedIconPath: "/static/icon_main_tab_home_selected.png",
  27. text: "首页"
  28. }, {
  29. pagePath: "/pages/activity/index",
  30. iconPath: "/static/icon_main_tab_benefit.png",
  31. selectedIconPath: "/static/icon_main_tab_benefit_selected.png",
  32. text: "福利"
  33. },
  34. {
  35. pagePath: "/pages/card/index",
  36. iconPath: "/static/icon_main_tab_mid.png",
  37. selectedIconPath: "/static/icon_main_tab_mid.png",
  38. text: "抽卡机"
  39. },
  40. {
  41. pagePath: "/pages/cabinet/index",
  42. iconPath: "/static/icon_main_tab_cabinet.png",
  43. selectedIconPath: "/static/icon_main_tab_cabinet_selected.png",
  44. text: "盒柜"
  45. },
  46. {
  47. pagePath: "/pages/profile/index",
  48. iconPath: "/static/icon_main_tab_profile.png",
  49. selectedIconPath: "/static/icon_main_tab_profile_selected.png",
  50. text: "我的"
  51. }
  52. ];
  53. const goPages = (pagePath: string, index: number) => {
  54. if (index !== props.selectIndex) {
  55. uni.switchTab({
  56. url: pagePath
  57. });
  58. }
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .tab_bar {
  63. width: 100vw;
  64. bottom: 0;
  65. position: fixed;
  66. background-color: #fff;
  67. // 自动计算底部安全距离
  68. padding-bottom: constant(safe-area-inset-bottom);
  69. padding-bottom: env(safe-area-inset-bottom);
  70. .tabbarBox {
  71. padding: 18rpx 16rpx 0;
  72. box-sizing: border-box;
  73. display: flex;
  74. justify-content: space-between;
  75. .handleBox {
  76. flex: 1;
  77. position: relative;
  78. display: flex;
  79. flex-direction: column;
  80. align-items: center;
  81. .menuBox {
  82. width: 100%;
  83. height: 100%;
  84. display: flex;
  85. flex-direction: column;
  86. align-items: center;
  87. .img {
  88. width: 66rpx;
  89. height: 66rpx;
  90. }
  91. }
  92. }
  93. }
  94. .tabbarBoxImg {
  95. width: 100%;
  96. height: 62rpx;
  97. position: absolute;
  98. top: -52rpx;
  99. left: 0;
  100. }
  101. }
  102. .Text {
  103. font-size: 20rpx;
  104. text-align: center;
  105. color: #9da5b3;
  106. font-weight: 600;
  107. letter-spacing: 6rpx;
  108. padding-left: 6rpx;
  109. box-sizing: border-box;
  110. }
  111. .TextColor {
  112. @extend .Text;
  113. color: #000;
  114. }
  115. .tabbarBox {
  116. :nth-child(3) .menuBoxText {
  117. color: #000000 !important;
  118. font-size: 26rpx;
  119. }
  120. :nth-child(3) .img {
  121. width: 140rpx !important;
  122. height: 140rpx !important;
  123. margin-top: -78rpx;
  124. }
  125. }
  126. </style>

在需要用的组件中直接引入使用并传入下标

手动隐藏原生tabbar

实现效果

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

闽ICP备14008679号