u-badge.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view v-if="show" class="u-badge-box" :class="[isDot ? 'u-badge-dot' : 'u-badge', size == 'mini' ? 'u-badge-mini' : '']" :style="[{
  3. top: offset[0] + 'rpx',
  4. right: offset[1] + 'rpx',
  5. fontSize: fontSize + 'rpx',
  6. position: absolute ? 'absolute' : 'static',
  7. color: color,
  8. backgroundColor: backgroundColor
  9. }, boxStyle]">
  10. {{showText}}
  11. </view>
  12. </template>
  13. <script>
  14. /**
  15. * badge 角标
  16. * @description 本组件一般用于展示头像的地方,如个人中心,或者评论列表页的用户头像展示等场所。
  17. * @tutorial https://www.uviewui.com/components/badge.html
  18. * @property {String Number} count 展示的数字,大于 overflowCount 时显示为 ${overflowCount}+,为0且show-zero为false时隐藏
  19. * @property {Boolean} is-dot 不展示数字,只有一个小点(默认false)
  20. * @property {Boolean} absolute 组件是否绝对定位,为true时,offset参数才有效(默认true)
  21. * @property {String Number} overflow-count 展示封顶的数字值(默认99)
  22. * @property {String} type 使用预设的背景颜色(默认error)
  23. * @property {Boolean} show-zero 当数值为 0 时,是否展示 Badge(默认false)
  24. * @property {String} size Badge的尺寸,设为mini会得到小一号的Badge(默认default)
  25. * @property {Array} offset 设置badge的位置偏移,格式为 [x, y],也即设置的为top和right的值,单位rpx。absolute为true时有效(默认[20, 20])
  26. * @property {String} color 字体颜色(默认#ffffff)
  27. * @property {String} bgColor 背景颜色,优先级比type高,如设置,type参数会失效
  28. * @property {Boolean} is-center 组件中心点是否和父组件右上角重合,优先级比offset高,如设置,offset参数会失效(默认false)
  29. * @example <u-badge type="error" count="7"></u-badge>
  30. */
  31. export default {
  32. name: 'u-badge',
  33. props: {
  34. // primary,warning,success,error,info
  35. type: {
  36. type: String,
  37. default: 'error'
  38. },
  39. // default, mini
  40. size: {
  41. type: String,
  42. default: 'default'
  43. },
  44. //是否是圆点
  45. isDot: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 显示的数值内容
  50. count: {
  51. type: [Number, String],
  52. },
  53. // 展示封顶的数字值
  54. overflowCount: {
  55. type: Number,
  56. default: 99
  57. },
  58. // 当数值为 0 时,是否展示 Badge
  59. showZero: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // 位置偏移
  64. offset: {
  65. type: Array,
  66. default: () => {
  67. return [20, 20]
  68. }
  69. },
  70. // 是否开启绝对定位,开启了offset才会起作用
  71. absolute: {
  72. type: Boolean,
  73. default: true
  74. },
  75. // 字体大小
  76. fontSize: {
  77. type: [String, Number],
  78. default: '24'
  79. },
  80. // 字体演示
  81. color: {
  82. type: String,
  83. default: '#ffffff'
  84. },
  85. // badge的背景颜色
  86. bgColor: {
  87. type: String,
  88. default: ''
  89. },
  90. // 是否让badge组件的中心点和父组件右上角重合,配置的话,offset将会失效
  91. isCenter: {
  92. type: Boolean,
  93. default: false
  94. }
  95. },
  96. computed: {
  97. // 是否将badge中心与父组件右上角重合
  98. boxStyle() {
  99. let style = {};
  100. if(this.isCenter) {
  101. style.top = 0;
  102. style.right = 0;
  103. // Y轴-50%,意味着badge向上移动了badge自身高度一半,X轴50%,意味着向右移动了自身宽度一半
  104. style.transform = "translateY(-50%) translateX(50%)";
  105. } else {
  106. style.top = this.offset[0] + 'rpx';
  107. style.right = this.offset[1] + 'rpx';
  108. style.transform = "translateY(0) translateX(0)";
  109. }
  110. // 如果尺寸为mini,后接上scal()
  111. if(this.size == 'mini') {
  112. style.transform = style.transform + " scale(0.8)";
  113. }
  114. return style;
  115. },
  116. // isDot类型时,不显示文字
  117. showText() {
  118. if(this.isDot) return '';
  119. else {
  120. if(this.count > this.overflowCount) return `${this.overflowCount}+`;
  121. else return this.count;
  122. }
  123. },
  124. // 是否显示组件
  125. show() {
  126. // 如果count的值为0,并且showZero设置为false,不显示组件
  127. if(this.count == 0 && this.showZero == false) return false;
  128. else return true;
  129. },
  130. // 获取背景颜色,如果定义了bgColor参数,就放弃type主题色
  131. backgroundColor() {
  132. return this.bgColor ? this.bgColor : this.$u.color[this.type];
  133. }
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. .u-badge-box {
  139. display: inline-flex;
  140. justify-content: center;
  141. align-items: center;
  142. }
  143. .u-badge {
  144. line-height: 24rpx;
  145. padding: 4rpx 8rpx;
  146. border-radius: 100rpx;
  147. }
  148. .u-badge-dot {
  149. height: 16rpx;
  150. width: 16rpx;
  151. border-radius: 100rpx;
  152. line-height: 1;
  153. }
  154. .u-badge-mini {
  155. transform: scale(0.8);
  156. transform-origin: center center;
  157. }
  158. // .u-primary {
  159. // background: $u-type-primary;
  160. // color: #fff;
  161. // }
  162. // .u-error {
  163. // background: $u-type-error;
  164. // color: #fff;
  165. // }
  166. // .u-warning {
  167. // background: $u-type-warning;
  168. // color: #fff;
  169. // }
  170. // .u-success {
  171. // background: $u-type-success;
  172. // color: #fff;
  173. // }
  174. // .u-black {
  175. // background: #585858;
  176. // color: #fff;
  177. // }
  178. .u-info {
  179. background: $u-type-info;
  180. color: #fff;
  181. }
  182. </style>