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