u-avatar-group.vue 3.0 KB

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