cate-tab.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view class="cate-tab">
  3. <view
  4. class="cate-tab-wrapper flex aic"
  5. :style="[{ position: noFixed ? 'static' : 'fixed' }]"
  6. >
  7. <view
  8. class="tab-item"
  9. :class="'tab-item-' + index"
  10. :style="itemStyle"
  11. v-for="(item, index) in tabs"
  12. :key="index"
  13. @click="tabHandle(item, index)"
  14. >
  15. <view
  16. class="tab-name ellipsis"
  17. :style="curCOM === index ? selectStyle : textStyle"
  18. >{{ item.name }}</view
  19. >
  20. <view class="tab-line" v-if="curCOM === index"></view>
  21. <!-- <view v-if="item.num && item.num > 0" :style="{width: item.num < 100? '34rpx': 'auto', padding:item.num < 100? '0': '0 10rpx', borderRadius: item.num < 100 ? '50%' : '20rpx'}" class="count">{{item.num < 99 ? item.num : '99+'}}</view> -->
  22. </view>
  23. </view>
  24. <view class="placeholder" v-if="!noFixed"></view>
  25. </view>
  26. </template>
  27. <script>
  28. import { toRpx } from "@/utils/calculate.js";
  29. export default {
  30. props: {
  31. noFixed: Boolean, // 不需要定位
  32. cur: {
  33. // 当前选中tab
  34. type: Number,
  35. default: 0,
  36. },
  37. itemStyle: {
  38. type: Object,
  39. },
  40. selectStyle: {
  41. type: Object,
  42. default: () => {
  43. return {
  44. fontSize: "40rpx",
  45. fontWeight: "bold",
  46. color: "#152129",
  47. };
  48. },
  49. },
  50. textStyle: {
  51. // 字体大小
  52. type: Object,
  53. default: () => {
  54. return {
  55. fontSize: "32rpx",
  56. color: "#707476",
  57. };
  58. },
  59. },
  60. tabs: Array, // 对应tab数据,name--tab文案,value--对应值,建议统一用value num--数标
  61. top: {
  62. // 距离顶部位置,不包括导航
  63. type: Number,
  64. default: 0,
  65. },
  66. },
  67. data() {
  68. return {
  69. lineLeft: 0, // 底部线条左定位值
  70. lineWidth: 0, // 底部线条宽度
  71. };
  72. },
  73. computed: {
  74. curCOM: {
  75. get() {
  76. return this.cur;
  77. },
  78. set(val) {
  79. this.$emit("update:cur", val);
  80. },
  81. },
  82. },
  83. methods: {
  84. async tabHandle(item, index) {
  85. if (this.curCOM === index) {
  86. return;
  87. }
  88. this.$emit("tabHandle", item, index);
  89. this.curCOM = index;
  90. },
  91. },
  92. };
  93. </script>
  94. <style lang="scss" scoped>
  95. .cate-tab {
  96. .cate-tab-wrapper {
  97. position: fixed;
  98. left: 0;
  99. padding: 0 40rpx;
  100. height: 100rpx;
  101. width: 100%;
  102. z-index: 99;
  103. line-height: 100rpx;
  104. background-color: #fff;
  105. // border-bottom: 1rpx solid $bg-color-main;
  106. // justify-content: space-around;
  107. .tab-line {
  108. position: absolute;
  109. bottom: 30rpx;
  110. left: 0;
  111. width: 100%;
  112. background-color: $color-main;
  113. border-radius: 4rpx;
  114. height: 12rpx;
  115. z-index: -1;
  116. transition: all ease-out 0.3s;
  117. }
  118. .tab-item {
  119. position: relative;
  120. font-size: 32rpx;
  121. }
  122. .count {
  123. position: absolute;
  124. right: -20rpx;
  125. top: 14rpx;
  126. border: 1rpx solid #fff;
  127. z-index: 20;
  128. background-color: $color-orange;
  129. color: #fff;
  130. font-size: 20rpx;
  131. height: 34rpx;
  132. display: flex;
  133. justify-content: center;
  134. align-content: center;
  135. text-align: center;
  136. }
  137. }
  138. .placeholder {
  139. height: 101rpx;
  140. }
  141. }
  142. </style>