效果如下图:在线预览
APIs
Avatar
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
shape | 指定头像的形状 | ‘circle’ | ‘square’ | ‘circle’ |
size | 设置头像的大小,number 类型时单位 px | number | ‘large’ | ‘small’ | ‘default’ | Responsive | ‘default’ |
src | 图片类头像资源地址 | string | undefined |
alt | 图片无法显示时的替代文本 | string | undefined |
icon | 设置头像的图标 | VNode | slot | undefined |
Responsive Type
名称 | 说明 | 类型 | 默认值 |
---|---|---|---|
xs | <576px 响应式栅格 | number | undefined |
sm | ≥576px 响应式栅格 | number | undefined |
md | ≥768px 响应式栅格 | number | undefined |
lg | ≥992px 响应式栅格 | number | undefined |
xl | ≥1200px 响应式栅格 | number | undefined |
xxl | ≥1600px 响应式栅格 | number | undefined |
其中引入使用了工具函数:throttle 节流、useEventListener 监听事件
创建头像组件Avatar.vue
其中引入使用了以下工具函数:
<script setup lang="ts">
import { ref, computed } from 'vue'
import type { VNode, Slot } from 'vue'
import { useEventListener, useSlotsExist } from '../utils'
interface Responsive {
xs?: number // <576px 响应式栅格
sm?: number // ≥576px 响应式栅格
md?: number // ≥768px 响应式栅格
lg?: number // ≥992px 响应式栅格
xl?: number // ≥1200px 响应式栅格
xxl?: number // ≥1600px 响应式栅格
}
interface Props {
shape?: 'circle' | 'square' // 指定头像的形状
size?: number | 'small' | 'default' | 'large' | Responsive // 设置头像的大小,number 类型时单位 px
src?: string // 图片类头像资源地址
alt?: string // 图片无法显示时的替代文本
icon?: VNode | Slot // 设置头像的图标
}
const props = withDefaults(defineProps<Props>(), {
shape: 'circle',
size: 'default',
src: undefined,
alt: undefined,
icon: undefined
})
const viewportWidth = ref(window.innerWidth)
function getViewportWidth() {
viewportWidth.value = window.innerWidth
}
useEventListener(window, 'resize', getViewportWidth)
const slotsExist = useSlotsExist(['default', 'icon'])
const showIcon = computed(() => {
if (!props.src) {
return Boolean(slotsExist.icon || props.icon)
}
return false
})
const avatarStyle = computed(() => {
if (typeof props.size === 'number') {
if (showIcon.value) {
return {
width: props.size + 'px',
height: props.size + 'px',
lineHeight: props.size + 'px',
fontSize: props.size / 2 + 'px'
}
} else {
return {
width: props.size + 'px',
height: props.size + 'px',
lineHeight: props.size + 'px',
fontSize: '18px'
}
}
}
if (typeof props.size === 'object') {
let size = 32
if (viewportWidth.value >= 1600 && props.size.xxl) {
size = props.size.xxl
} else if (viewportWidth.value >= 1200 && props.size.xl) {
size = props.size.xl
} else if (viewportWidth.value >= 992 && props.size.lg) {
size = props.size.lg
} else if (viewportWidth.value >= 768 && props.size.md) {
size = props.size.md
} else if (viewportWidth.value >= 576 && props.size.sm) {
size = props.size.sm
} else if (viewportWidth.value < 576 && props.size.xs) {
size = props.size.xs
}
return {
width: size + 'px',
height: size + 'px',
lineHeight: size + 'px',
fontSize: size / 2 + 'px'
}
}
return {}
})
const showStr = computed(() => {
if (!props.src && !showIcon.value) {
return slotsExist.default
}
return false
})
const strStyle = computed(() => {
if (typeof props.size === 'string') {
return {
transform: `scale(1) translateX(-50%)`
}
}
if (typeof props.size === 'number') {
const scale = Math.min(1, Math.max(1 / 45, (1 + (props.size - 9) * 1) / 45))
return {
lineHeight: props.size + 'px',
transform: `scale(${scale}) translateX(-50%)`
}
}
return {}
})
</script>
<template>
<div
class="m-avatar"
:class="[
`avatar-${shape}`,
{
[`avatar-${size}`]: typeof size === 'string',
'avatar-image': src
}
]"
:style="avatarStyle"
>
<img v-if="src" class="avatar-image" :src="src" :alt="alt" />
<slot name="icon" v-if="!src && showIcon">
<component :is="icon" />
</slot>
<span v-if="!src && !showIcon && showStr" class="avatar-string" :style="strStyle">
<slot></slot>
</span>
</div>
</template>
<style lang="less" scoped>
.m-avatar {
position: relative;
display: inline-flex;
justify-content: center;
align-items: center;
vertical-align: middle;
width: 32px;
height: 32px;
border-radius: 50%;
font-size: 14px;
color: #fff;
line-height: 30px;
background: rgba(0, 0, 0, 0.25);
border: 1px solid transparent;
overflow: hidden;
white-space: nowrap;
&.avatar-square {
border-radius: 6px;
}
.avatar-image {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
:deep(svg) {
fill: currentColor;
}
.avatar-string {
position: absolute;
left: 50%;
transform-origin: 0 center;
}
}
.avatar-large {
font-size: 24px;
width: 40px;
height: 40px;
line-height: 38px;
border-radius: 50%;
.avatar-icon {
font-size: 24px;
}
&.avatar-square {
border-radius: 8px;
}
}
.avatar-default {
.avatar-icon {
font-size: 18px;
}
}
.avatar-small {
font-size: 14px;
width: 24px;
height: 24px;
line-height: 22px;
border-radius: 50%;
.avatar-icon {
font-size: 14px;
}
&.avatar-square {
border-radius: 4px;
}
}
.avatar-image {
background: transparent;
}
</style>
在要使用的页面引入
其中引入使用了以下组件:
<script setup lang="ts">
import Avatar from './Avatar.vue'
import { h } from 'vue'
import { UserOutlined, TeamOutlined, SketchOutlined } from '@ant-design/icons-vue'
</script>
<template>
<div>
<h1>{{ $route.name }} {{ $route.meta.title }}</h1>
<h2 class="mt30 mb10">基本使用</h2>
<Space vertical>
<Space align="center">
<Avatar :size="64">
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar size="large">
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar>
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar size="small">
<template #icon>
<UserOutlined />
</template>
</Avatar>
</Space>
<Space align="center">
<Avatar shape="square" :size="64">
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar shape="square" size="large">
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar shape="square">
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar shape="square" size="small">
<template #icon>
<UserOutlined />
</template>
</Avatar>
</Space>
</Space>
<h2 class="mt30 mb10">自定义图标类型</h2>
<Space align="center">
<Avatar :icon="() => h(TeamOutlined)" />
<Avatar>
<template #icon>
<UserOutlined />
</template>
</Avatar>
<Avatar>U</Avatar>
<Avatar :size="40">USER</Avatar>
<Avatar :size="40" src="https://cdn.jsdelivr.net/gh/themusecatcher/resources@0.0.5/1.jpg" />
<Avatar style="color: #f56a00; background-color: #fde3cf">U</Avatar>
<Avatar style="background-color: #87d068">
<template #icon>
<UserOutlined />
</template>
</Avatar>
</Space>
<h2 class="mt30 mb10">带徽标的头像</h2>
<Space>
<Badge :count="1">
<Avatar shape="square">
<template #icon>
<UserOutlined />
</template>
</Avatar>
</Badge>
<Badge dot>
<Avatar shape="square">
<template #icon>
<UserOutlined />
</template>
</Avatar>
</Badge>
</Space>
<h2 class="mt30 mb10">响应式尺寸</h2>
<Avatar :size="{ xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 }">
<template #icon>
<SketchOutlined />
</template>
</Avatar>
</div>
</template>