3
0

u-avatar-group.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view class="u-avatar-group">
  3. <view
  4. class="u-avatar-group__item"
  5. v-for="(item, index) in showUrl"
  6. :key="index"
  7. :style="{
  8. marginLeft: index === 0 ? 0 : addUnit(-size * gap)
  9. }"
  10. >
  11. <u-avatar
  12. :size="size"
  13. :shape="shape"
  14. :mode="mode"
  15. :src="testObject(item) ? keyName && item[keyName] || item.url : item"
  16. ></u-avatar>
  17. <view
  18. class="u-avatar-group__item__show-more"
  19. v-if="showMore && index === showUrl.length - 1 && (urls.length > maxCount || extraValue > 0)"
  20. @tap="clickHandler"
  21. >
  22. <up-text
  23. color="#ffffff"
  24. :size="size * 0.4"
  25. :text="`+${extraValue || urls.length - showUrl.length}`"
  26. align="center"
  27. customStyle="justify-content: center"
  28. ></up-text>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import { props } from './props';
  35. import { mpMixin } from '../../libs/mixin/mpMixin';
  36. import { mixin } from '../../libs/mixin/mixin';
  37. import { addUnit } from '../../libs/function/index';
  38. import test from '../../libs/function/test';
  39. /**
  40. * AvatarGroup 头像组
  41. * @description 本组件一般用于展示头像的地方,如个人中心,或者评论列表页的用户头像展示等场所。
  42. * @tutorial https://ijry.github.io/uview-plus/components/avatar.html
  43. *
  44. * @property {Array} urls 头像图片组 (默认 [] )
  45. * @property {String | Number} maxCount 最多展示的头像数量 ( 默认 5 )
  46. * @property {String} shape 头像形状( 'circle' (默认) | 'square' )
  47. * @property {String} mode 图片裁剪模式(默认 'scaleToFill' )
  48. * @property {Boolean} showMore 超出maxCount时是否显示查看更多的提示 (默认 true )
  49. * @property {String | Number} size 头像大小 (默认 40 )
  50. * @property {String} keyName 指定从数组的对象元素中读取哪个属性作为图片地址
  51. * @property {String | Number} gap 头像之间的遮挡比例(0.4代表遮挡40%) (默认 0.5 )
  52. * @property {String | Number} extraValue 需额外显示的值
  53. * @event {Function} showMore 头像组更多点击
  54. * @example <u-avatar-group:urls="urls" size="35" gap="0.4" ></u-avatar-group:urls=>
  55. */
  56. export default {
  57. name: 'u-avatar-group',
  58. mixins: [mpMixin, mixin, props],
  59. data() {
  60. return {
  61. }
  62. },
  63. computed: {
  64. showUrl() {
  65. return this.urls.slice(0, this.maxCount)
  66. }
  67. },
  68. emits: ["showMore"],
  69. methods: {
  70. addUnit,
  71. testObject: test.object,
  72. clickHandler() {
  73. this.$emit('showMore')
  74. }
  75. },
  76. }
  77. </script>
  78. <style lang="scss" scoped>
  79. @import "../../libs/css/components.scss";
  80. .u-avatar-group {
  81. @include flex;
  82. &__item {
  83. margin-left: -10px;
  84. position: relative;
  85. &--no-indent {
  86. // 如果你想质疑作者不会使用:first-child,说明你太年轻,因为nvue不支持
  87. margin-left: 0;
  88. }
  89. &__show-more {
  90. position: absolute;
  91. top: 0;
  92. bottom: 0;
  93. left: 0;
  94. right: 0;
  95. background-color: rgba(0, 0, 0, 0.3);
  96. @include flex;
  97. align-items: center;
  98. justify-content: center;
  99. border-radius: 100px;
  100. }
  101. }
  102. }
  103. </style>