h-tabs.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view class="h-tabs" :style="{ '--active-color': activeColor }">
  3. <view v-for="item in tabs" :key="item[valueKey]" :class="{ active: item[valueKey] === currentValue }"
  4. :style="[item[valueKey] === currentValue ? item.activeStyle : item.style]" class="h-tabs-item"
  5. @click="handleTabClick(item)">
  6. {{ item[labelKey] }}
  7. <!-- 显示红点 -->
  8. <view v-if="item.unreadCount > 0" class="h-tabs-red-dot"></view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'HTabs',
  15. props: {
  16. tabs: {
  17. type: Array,
  18. default: () => []
  19. },
  20. value: {
  21. type: [String, Number],
  22. default: ''
  23. },
  24. valueKey: {
  25. type: String,
  26. default: 'type'
  27. },
  28. labelKey: {
  29. type: String,
  30. default: 'title'
  31. },
  32. activeColor: {
  33. type: String,
  34. default: '#2A70D1'
  35. }
  36. },
  37. data() {
  38. return {
  39. currentValue: this.value
  40. };
  41. },
  42. watch: {
  43. value(newVal) {
  44. this.currentValue = newVal;
  45. }
  46. },
  47. methods: {
  48. handleTabClick(item) {
  49. this.currentValue = item[this.valueKey];
  50. this.$emit('input', this.currentValue);
  51. this.$emit('change', this.currentValue, item);
  52. }
  53. }
  54. };
  55. </script>
  56. <style lang="scss" scoped>
  57. .h-tabs {
  58. display: flex;
  59. padding: 0 0 32rpx;
  60. font-size: 28rpx;
  61. color: #666666;
  62. line-height: 32rpx;
  63. gap: 48rpx;
  64. width: 100%;
  65. // 默认样式
  66. .h-tabs-item {
  67. position: relative;
  68. cursor: pointer;
  69. padding: 0 8rpx;
  70. &.active {
  71. font-size: 36rpx;
  72. color: var(--active-color);
  73. line-height: 42rpx;
  74. &:after {
  75. content: "";
  76. position: absolute;
  77. bottom: -12rpx;
  78. left: 0;
  79. width: 50%;
  80. height: 6rpx;
  81. background: var(--active-color);
  82. border-radius: 12rpx;
  83. transition: all 0.3s ease;
  84. }
  85. }
  86. }
  87. // 红点样式
  88. .h-tabs-red-dot {
  89. position: absolute;
  90. top: -4rpx;
  91. right: 0;
  92. width: 16rpx;
  93. height: 16rpx;
  94. background-color: #FF4D4F;
  95. border-radius: 50%;
  96. transform: translateX(50%);
  97. }
  98. }
  99. </style>