cate-tab.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: "36rpx",
  45. fontWeight: "600",
  46. color: "#1780E2",
  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 45rpx;
  100. height: 100rpx;
  101. width: 100%;
  102. z-index: 99;
  103. line-height: 100rpx;
  104. background-color: #fff;
  105. overflow-x: scroll;
  106. // border-bottom: 1rpx solid $bg-color-main;
  107. // justify-content: space-around;
  108. .tab-line {
  109. position: absolute;
  110. bottom: 0;
  111. left: 0;
  112. width: 100%;
  113. background-color: $color-main;
  114. border-radius: 4rpx;
  115. height: 8rpx;
  116. transition: all ease-out 0.3s;
  117. }
  118. .tab-item {
  119. position: relative;
  120. font-size: 32rpx;
  121. // margin-right: 50rpx;
  122. }
  123. .count {
  124. position: absolute;
  125. right: -20rpx;
  126. top: 14rpx;
  127. border: 1rpx solid #fff;
  128. z-index: 20;
  129. background-color: $color-orange;
  130. color: #fff;
  131. font-size: 20rpx;
  132. height: 34rpx;
  133. display: flex;
  134. justify-content: center;
  135. align-content: center;
  136. text-align: center;
  137. }
  138. }
  139. .cate-tab-wrapper::-webkit-scrollbar{
  140. display:none,
  141. }
  142. .placeholder {
  143. height: 101rpx;
  144. }
  145. }
  146. </style>