tn-badge.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view
  3. class="tn-badge-class tn-badge"
  4. :class="[
  5. backgroundColorClass,
  6. fontColorClass,
  7. badgeClass
  8. ]"
  9. :style="[badgeStyle]"
  10. @click="handleClick"
  11. >
  12. <slot v-if="!dot"></slot>
  13. </view>
  14. </template>
  15. <script>
  16. import componentsColorMixin from '../../libs/mixin/components_color.js'
  17. export default {
  18. mixins: [componentsColorMixin],
  19. name: 'tn-badge',
  20. props: {
  21. // 序号
  22. index: {
  23. type: [Number, String],
  24. default: '0'
  25. },
  26. // 徽章的大小 rpx
  27. radius: {
  28. type: Number,
  29. default: 0
  30. },
  31. // 内边距
  32. padding: {
  33. type: String,
  34. default: ''
  35. },
  36. // 外边距
  37. margin: {
  38. type: String,
  39. default: ''
  40. },
  41. // 是否为一个点
  42. dot: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 是否使用绝对定位
  47. absolute: {
  48. type: Boolean,
  49. default: false
  50. },
  51. // top
  52. top: {
  53. type: [String, Number],
  54. default: ''
  55. },
  56. // right
  57. right: {
  58. type: [String, Number],
  59. default: ''
  60. },
  61. // 居中 对齐右上角
  62. translateCenter: {
  63. type: Boolean,
  64. default: true
  65. }
  66. },
  67. computed: {
  68. badgeClass() {
  69. let clazz = ''
  70. if (this.dot) {
  71. clazz += ' tn-badge--dot'
  72. }
  73. if (this.absolute) {
  74. clazz += ' tn-badge--absolute'
  75. if (this.translateCenter) {
  76. clazz += ' tn-badge--center-position'
  77. }
  78. }
  79. return clazz
  80. },
  81. badgeStyle() {
  82. let style = {}
  83. if (this.radius !== 0) {
  84. style.width = this.radius + 'rpx'
  85. style.height = this.radius + 'rpx'
  86. style.lineHeight = this.radius + 'rpx'
  87. // style.borderRadius = (this.radius * 8) + 'rpx'
  88. }
  89. if (this.padding) {
  90. style.padding = this.padding
  91. }
  92. if (this.margin) {
  93. style.margin = this.margin
  94. }
  95. if (this.fontColorStyle) {
  96. style.color = this.fontColorStyle
  97. }
  98. if (this.fontSize) {
  99. style.fontSize = this.fontSize + this.fontUnit
  100. }
  101. if (this.backgroundColorStyle) {
  102. style.backgroundColor = this.backgroundColorStyle
  103. }
  104. if (this.top) {
  105. style.top = this.$t.string.getLengthUnitValue(this.top)
  106. }
  107. if (this.right) {
  108. style.right = this.$t.string.getLengthUnitValue(this.right)
  109. }
  110. return style
  111. },
  112. },
  113. data() {
  114. return {
  115. }
  116. },
  117. methods: {
  118. // 处理点击事件
  119. handleClick() {
  120. this.$emit('click', {
  121. index: Number(this.index)
  122. })
  123. this.$emit('tap', {
  124. index: Number(this.index)
  125. })
  126. },
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .tn-badge {
  132. width: auto;
  133. height: auto;
  134. box-sizing: border-box;
  135. display: flex;
  136. align-items: center;
  137. justify-content: center;
  138. z-index: 10;
  139. font-size: 20rpx;
  140. background-color: #FFFFFF;
  141. // color: #FFFFFF;
  142. border-radius: 100rpx;
  143. padding: 4rpx 12rpx;
  144. line-height: initial;
  145. &--dot {
  146. width: 8rpx;
  147. height: 8rpx;
  148. border-radius: 50%;
  149. padding: 0;
  150. }
  151. &--absolute {
  152. position: absolute;
  153. top: 0;
  154. right: 0;
  155. }
  156. &--center-position {
  157. transform: translate(50%, -50%);
  158. }
  159. }
  160. </style>