vue3仿飞书头像,根据不同名称生成不同的头像背景色

效果展示:
传递三个参数:
  • name:要显示的名称;
  • size:头像的大小;
  • cutNum:分割当前名称的最后几位数;
代码如下:
html 复制代码
<template>
	<div
		:style="{
			color: '#fff',
			borderRadius: '50%',
			width: size + 'px',
			height: size + 'px',
			display: 'block',
			textAlign: 'center',
			lineHeight: size + 'px',
			fontSize: size * 0.4 + 'px',
			background: color(name)
		}"
	>
		{{ name.slice(-cutNum) }}
	</div>
</template>

<script lang="ts" setup>
defineProps({
	name: {
		type: String,
		default: ""
	},
	size: {
		type: Number,
		default: 16
	},
	cutNum: {
		type: Number,
		default: 1
	}
});

const color = (name: string) => {
	if (name) {
		let num = "";
		for (let i = 0; i < name.length; i++) {
			num += name[i].charCodeAt(0).toString();
		}
		const r = Math.min(100 + (parseInt(num.slice(0, 2), 10) % 55), 255);
		const g = Math.min(100 + (parseInt(num.slice(2, 4), 10) % 55), 255);
		const b = Math.min(100 + (parseInt(num.slice(4, 6), 10) % 55), 255);
		return `rgb(${r}, ${g}, ${b})`;
	} else {
		return "rgb(125, 125, 125)";
	}
};
</script>
使用代码如下:
复制代码
<AuthorImage name="铁甲小宝" :size="74" :cut-num="2"></AuthorImage>
相关推荐
即安莉1 小时前
从 /ping 到 pong:5 分钟实现飞书机器人与本地大模型的深度集成
机器人·飞书
He1955011 小时前
openclaw配置飞书
飞书·openclaw·openclaw配置飞书
江南西肥肥2 小时前
养虾日记[特殊字符]:多Agent在飞书群辩论--踩坑篇
网络·飞书·openclaw
16Miku3 小时前
OpenClaw-Linux+飞书官方Plugin安装指南
linux·ai·飞书·agent·openclaw·feishu
Imagine Miracle5 小时前
【Openclaw】安装部署Openclaw,并连接到飞书
飞书·openclaw
想要AC的sjh6 小时前
【硬核实战】Mac mini M4 部署 OpenClaw + Ollama 本地大模型:从零到一打通飞书机器人
macos·机器人·飞书
suixinm6 小时前
基于 OpenClaw 多 Agent 系统设计与接入飞书运用实践
飞书·openclaw·机器人助力
yumgpkpm7 小时前
“养龙虾”OpenClaw配置一个“每日新闻摘要”skill,推送到飞书或钉钉。+华为泰山2280(现货)
数据库·钉钉·飞书
丶伯爵式7 小时前
OpenClaw 接入飞书 (2026年3月11日)
ai·飞书·ai助手·openclaw·大龙虾
孪生质数-8 小时前
OpenClaw安装飞书官方插件教程
飞书·openclaw