Avatar头像

Avatar 头像

用来代表用户或事物,支持图片、图标或字符展示。

设计师专属

安装 Kitchen Sketch 插件 💎,一键填充高逼格头像和文本。

代码演示

基本

头像有三种尺寸,两种形状可选。

<​template>  <​a-avatar :size="64">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar size="large">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar>    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar size="small">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​br />  <​a-avatar shape="square" :size="64">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar shape="square" size="large">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar shape="square">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar>  <​a-avatar shape="square" size="small">    <​template #icon><​UserOutlined /><​/template>  <​/a-avatar><​/template><​script lang="ts">import { UserOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';export default defineComponent({  components: {    UserOutlined,  },});<​/script>

U改 变

自动调整字符大小

对于字符型的头像,当字符串较长时,字体大小可以根据头像宽度自动调整。

<​template>  <​a-avatar    shape="square"    size="large"    :style="{ backgroundColor: color, verticalAlign: 'middle' }"  >    {{ avatarValue }}  <​/a-avatar>  <​a-button    size="small"    :style="{ marginLeft: '16px', verticalAlign: 'middle' }"    @click="changeValue"  >    改变  <​/a-button><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';const UserList = ['U', 'Lucy', 'Tom', 'Edward'];const colorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];export default defineComponent({  setup() {    const avatarValue = ref(UserList[0]);    const color = ref(colorList[0]);    const changeValue = () => {      const index = UserList.indexOf(avatarValue.value);      avatarValue.value = index < UserList.length - 1 ? UserList[index + 1] : UserList[0];      color.value = index < colorList.length - 1 ? colorList[index + 1] : colorList[0];    };    return {      avatarValue,      color,      changeValue,    };  },});<​/script>

UUSERU

类型

支持三种类型:图片、Icon 以及字符,其中 Icon 和字符型可以自定义图标颜色及背景色。

<​template>  <​a-avatar>    <​template #icon>      <​UserOutlined />    <​/template>  <​/a-avatar>  <​a-avatar>U<​/a-avatar>  <​a-avatar>USER<​/a-avatar>  <​a-avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />  <​a-avatar style="color: #f56a00; backgroundcolor: #fde3cf">U<​/a-avatar>  <​a-avatar style="backgroundcolor: #87d068">    <​template #icon>      <​UserOutlined />    <​/template>  <​/a-avatar><​/template><​script lang="ts">import { UserOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';export default defineComponent({  components: {    UserOutlined,  },});<​/script>

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

带徽标的头像

通常用于消息提示。

<​template>  <​span style="margin-right: 24px">    <​a-badge :count="1">      <​a-avatar shape="square">        <​template #icon><​UserOutlined /><​/template>      <​/a-avatar>    <​/a-badge>  <​/span>  <​span>    <​a-badge dot>      <​a-avatar shape="square">        <​template #icon><​UserOutlined /><​/template>      <​/a-avatar>    <​/a-badge>  <​/span><​/template><​script lang="ts">import { UserOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';export default defineComponent({  components: {    UserOutlined,  },});<​/script><​style>#components-avatar-demo-badge .ant-avatar {  margin-top: 0;  margin-right: 0;}<​/style>

API

参数 说明 类型 默认值

| icon | 设置头像的图标类型,可设为 Icon 的 type 或 VNode | VNode | slot | - | |---|---|---|---|

shape 指定头像的形状 Enum{ ‘circle’, ‘square’ } circle

| size | 设置头像的大小 | number | Enum{ ‘large’, ‘small’, ‘default’ } | default | |---|---|---|---|

src 图片类头像的资源地址 string -
srcset 设置图片类头像响应式资源地址 string -
alt 图像无法显示时的替代文本 string -
loadError 图片加载失败的事件,返回 false 会关闭组件默认的 fallback 行为 () => boolean -