custom-tabbar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="custom-tabbar" v-if="isShowTabbar">
  3. <view v-for="(item, index) in list" :key="index" class="tabbar-item" @click="switchTab(index)">
  4. <!-- <view v-if="item.text === '新建'" class="menu-item">
  5. <image class="add_icon" :src="item.icon" mode="scaleToFill" />
  6. </view> -->
  7. <view class="menu-item">
  8. <image v-if="currentIndex === index" class="w-48 h-48" :src="item.check_icon" mode="scaleToFill" />
  9. <image v-else class="w-48 h-48" :src="item.icon" mode="scaleToFill" />
  10. <text class="mgt-4" :class="{ active: currentIndex === index }">{{ item.text }}</text>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. import { ref } from 'vue';
  17. // 当前选中的 Tab 索引
  18. const props = defineProps(["currentIndex"])
  19. // TabBar 列表
  20. const list = ref([{
  21. text: "书架",
  22. pagePath: "/pages/index/index",
  23. icon: '/static/image/tabbar/bookshelf.png',
  24. check_icon: '/static/image/tabbar/bookshelf_on.png'
  25. },
  26. {
  27. text: "书城",
  28. pagePath: "/pages/bookMall/index",
  29. icon: '/static/image/tabbar/bookMall.png',
  30. check_icon: '/static/image/tabbar/bookMall_on.png'
  31. },
  32. {
  33. text: "我的",
  34. pagePath: "/pages/my/index",
  35. icon: '/static/image/tabbar/my.png',
  36. check_icon: '/static/image/tabbar/my_on.png'
  37. },
  38. ]);
  39. // 定义 isShowTabbar 变量
  40. const isShowTabbar = ref(true);
  41. // // 监听 props.currentIndex 变化,控制 tabbar 显示状态
  42. // watch(() => props.currentIndex, (newIndex) => {
  43. // if (list.value[newIndex]?.text === "新建") {
  44. // isShowTabbar.value = false; // 隐藏 tabbar
  45. // } else {
  46. // isShowTabbar.value = true; // 显示 tabbar
  47. // }
  48. // });
  49. // 切换 Tab
  50. const switchTab = (index) => {
  51. /* if(index == 1) {
  52. uni.navigateTo({
  53. url: "/pages/creat/newBook",
  54. });
  55. } else { */
  56. uni.switchTab({
  57. url: list.value[index].pagePath,
  58. });
  59. // }
  60. };
  61. </script>
  62. <style scoped>
  63. .custom-tabbar {
  64. position: fixed;
  65. left: 0;
  66. right: 0;
  67. bottom: 0;
  68. z-index: 999;
  69. display: flex;
  70. height: 100rpx;
  71. /* 适配安全区 */
  72. padding-top: 10rpx;
  73. /* 默认 Android */
  74. padding-bottom: 10rpx;
  75. background-color: #ffffff;
  76. /* background-color: transparent; */
  77. justify-content: space-between;
  78. }
  79. /* 兼容 iOS 安全区域 */
  80. @supports (bottom: env(safe-area-inset-bottom)) {
  81. .custom-tabbar {
  82. height: calc(100rpx + env(safe-area-inset-bottom));
  83. padding-bottom: env(safe-area-inset-bottom);
  84. }
  85. }
  86. .tabbar-item {
  87. flex: 1;
  88. }
  89. .menu-item {
  90. position: relative;
  91. display: flex;
  92. flex-direction: column;
  93. align-items: center;
  94. color: #E5DCD6;
  95. font-size: 20rpx;
  96. }
  97. .add_icon {
  98. position: absolute;
  99. top: -30rpx;
  100. width: 96rpx;
  101. height: 96rpx;
  102. }
  103. .active {
  104. color: #FFA74F;
  105. }
  106. </style>