Vue3头像(Avatar)

76 篇文章 4 订阅
73 篇文章 3 订阅

效果如下图:在线预览

在这里插入图片描述

APIs

Avatar

参数说明类型默认值
shape指定头像的形状‘circle’ | ‘square’‘circle’
size设置头像的大小,number类型时单位 pxnumber | ‘large’ | ‘small’ | ‘default’ | Responsive‘default’
src图片类头像资源地址stringundefined
alt图片无法显示时的替代文本stringundefined
icon设置头像的图标VNode | slotundefined

Responsive Type

名称说明类型默认值
xs<576px 响应式栅格numberundefined
sm≥576px 响应式栅格numberundefined
md≥768px 响应式栅格numberundefined
lg≥992px 响应式栅格numberundefined
xl≥1200px 响应式栅格numberundefined
xxl≥1600px 响应式栅格numberundefined

其中引入使用了工具函数: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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值