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>
相关推荐
Amentos3 天前
飞书常用功能(留档)
飞书·飞书表格函数
ChiLi_Lin3 天前
Unity异常上报飞书工具
unity·游戏引擎·飞书
落笔画忧愁e3 天前
扣子Coze飞书多维表插件-列出全部数据表
java·服务器·飞书
百里香酚兰4 天前
【AI学习笔记】Coze工作流写入飞书多维表格(即:多维表格飞书官方插件使用教程)
笔记·学习·大模型·飞书·pe·coze
在人间负债^15 天前
飞书知识问答深度体验:企业AI应用落地的典范产品
人工智能·飞书
低代码布道师15 天前
集成飞书多维表格
飞书
懵逼的小黑子25 天前
飞书配置表数据同步到数据库中
数据库·飞书
涛涛讲AI1 个月前
如何设置飞书多维表格,可以在扣子平台上使用
飞书·智能体·扣子
呆萌的代Ma1 个月前
扣子流程图批量导入飞书多维表格
流程图·飞书·扣子
只会写bug的靓仔1 个月前
mac 设置飞书默认浏览器(解决系统设置默认浏览器无效)
macos·飞书