Vue3头像(Avatar)

80 篇文章 4 订阅
74 篇文章 3 订阅

效果如下图:

在这里插入图片描述

在线预览

APIs

Avatar

参数说明类型默认值
color头像的背景色string‘rgba(0, 0, 0, 0.25)’
shape指定头像的形状‘circle’ | ‘square’‘circle’
size设置头像的大小,number类型时单位 pxnumber | ‘small’ | ‘middle’ | ‘large’ | Responsive‘middle’
src图片类头像资源地址stringundefined
alt图片无法显示时的替代文本stringundefined
icon设置头像的图标VNode | slotundefined
href点击跳转的地址,指定此属性按钮的行为和 a 链接一致stringundefined
target相当于 a 标签的 target 属性,href 存在时生效‘self’ | ‘_blank’‘self’

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 {
  color?: string // 头像的背景色
  shape?: 'circle' | 'square' // 指定头像的形状
  size?: number | 'small' | 'middle' | 'large' | Responsive // 设置头像的大小,number 类型时单位 px
  src?: string // 图片类头像资源地址
  alt?: string // 图片无法显示时的替代文本
  icon?: VNode | Slot // 设置头像的图标
  href?: string // 点击跳转的地址,指定此属性按钮的行为和 a 链接一致
  target?: '_self' | '_blank' // 相当于 a 标签的 target 属性,href 存在时生效
}
const props = withDefaults(defineProps<Props>(), {
  color: 'rgba(0, 0, 0, 0.25)',
  shape: 'circle',
  size: 'middle',
  src: undefined,
  alt: undefined,
  icon: undefined,
  href: undefined,
  target: '_self'
})
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 {
        backgroundColor: props.color,
        width: `${props.size}px`,
        height: `${props.size}px`,
        lineHeight: `${props.size}px`,
        fontSize: `${props.size / 2}px`
      }
    } else {
      return {
        backgroundColor: props.color,
        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 {
      backgroundColor: props.color,
      width: `${size}px`,
      height: `${size}px`,
      lineHeight: `${size}px`,
      fontSize: `${size / 2}px`
    }
  }
  return {
    backgroundColor: props.color
  }
})
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>
  <component
    :is="href ? 'a' : 'div'"
    class="m-avatar"
    :class="[
      `avatar-${shape}`,
      {
        [`avatar-${size}`]: typeof size === 'string' && ['small', 'middle', 'large'].includes(size),
        'avatar-image': src,
        'avatar-link': href
      }
    ]"
    :style="avatarStyle"
    :href="href"
    :target="target"
  >
    <img v-if="src" class="image-item" :src="src" :alt="alt" />
    <slot v-if="!src && showIcon" name="icon">
      <component :is="icon" />
    </slot>
    <span v-if="!src && !showIcon && showStr" class="string-item" :style="strStyle">
      <slot></slot>
    </span>
  </component>
</template>
<style lang="less" scoped>
.m-avatar {
  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 14px;
  color: #fff;
  line-height: 30px;
  border: 1px solid transparent;
  overflow: hidden;
  white-space: nowrap;
  cursor: auto;
  outline: none;
  &:hover {
    color: #fff;
  }
  &.avatar-square {
    border-radius: 6px;
  }
  .image-item {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  :deep(svg) {
    width: 1em;
    height: 1em;
    fill: currentColor;
  }
  .string-item {
    position: absolute;
    left: 50%;
    transform-origin: 0 center;
  }
}
.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-middle {
  .avatar-icon {
    font-size: 18px;
  }
}
.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-image {
  background: transparent;
}
.avatar-link {
  cursor: pointer;
}
</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>
      <Avatar color="#1677ff">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar color="#ff6900" shape="square">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar color="#87d068">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
    </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>
    </Space>
    <h2 class="mt30 mb10">带徽标的头像</h2>
    <Space>
      <Badge :value="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>
    <h2 class="mt30 mb10">链接跳转</h2>
    <Tooltip tooltip="themusecatcher@github">
      <Avatar
        :size="36"
        src="https://github.com/themusecatcher.png"
        href="https://github.com/themusecatcher"
        target="_blank"
      />
    </Tooltip>
  </div>
</template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值