custom-tabbar.uvue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="custom-tabbar">
  3. <view v-for="(item, index) in tabList" :key="index" class="tab-item" @click="handleTabClick(index)">
  4. <image class="tab-icon" :src="currentIndex == index ? item.selectedIconPath : item.iconPath" mode="aspectFit"></image>
  5. <text :class="['tab-text', currentIndex == index ? 'tab-text-active' : '']">
  6. {{ item.text }}
  7. </text>
  8. </view>
  9. </view>
  10. </template>
  11. <script setup lang="uts">
  12. import { ref } from 'vue'
  13. // Props
  14. type Props = {
  15. current?: number
  16. }
  17. const props = withDefaults(defineProps<Props>(), {
  18. current: 0
  19. })
  20. // Tab 列表配置
  21. type TabItem = {
  22. pagePath: string
  23. text: string
  24. iconPath: string
  25. selectedIconPath: string
  26. }
  27. const tabList: TabItem[] = [
  28. {
  29. pagePath: '/pages/index/index',
  30. text: '首页',
  31. iconPath: '/static/images/custom-tabbar/11.png',
  32. selectedIconPath: '/static/images/custom-tabbar/22.png'
  33. },
  34. {
  35. pagePath: '/pages/order/index',
  36. text: '工单',
  37. iconPath: '/static/images/custom-tabbar/33.png',
  38. selectedIconPath: '/static/images/custom-tabbar/44.png'
  39. },
  40. {
  41. pagePath: '/pages/worktime/index',
  42. text: '工时',
  43. iconPath: '/static/images/custom-tabbar/77.png',
  44. selectedIconPath: '/static/images/custom-tabbar/88.png'
  45. },
  46. {
  47. pagePath: '/pages/score/index',
  48. text: '工分',
  49. iconPath: '/static/images/custom-tabbar/55.png',
  50. selectedIconPath: '/static/images/custom-tabbar/66.png'
  51. }
  52. ]
  53. // 当前选中索引
  54. const currentIndex = ref<number>(props.current)
  55. // Tab 点击事件
  56. const handleTabClick = (index: number): void => {
  57. if (index == currentIndex.value) {
  58. return
  59. }
  60. currentIndex.value = index
  61. const item = tabList[index]
  62. uni.redirectTo({
  63. url: item.pagePath
  64. })
  65. }
  66. </script>
  67. <style lang="scss">
  68. .custom-tabbar {
  69. position: fixed;
  70. bottom: 0;
  71. left: 0;
  72. right: 0;
  73. z-index: 1000;
  74. height: 150rpx;
  75. background-color: #ffffff;
  76. border-top: 1rpx solid #e5e5e5;
  77. flex-direction: row;
  78. justify-content: space-around;
  79. align-items: center;
  80. padding-bottom: env(safe-area-inset-bottom);
  81. }
  82. .tab {
  83. &-item {
  84. flex: 1;
  85. justify-content: center;
  86. align-items: center;
  87. }
  88. &-icon {
  89. width: 48rpx;
  90. height: 48rpx;
  91. margin-bottom: 8rpx;
  92. }
  93. &-text {
  94. font-size: 24rpx;
  95. color: #666666;
  96. &-active {
  97. color: #0081ff;
  98. }
  99. }
  100. }
  101. </style>