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>
相关推荐
聚梦小课堂5 天前
在n8n中清理单个飞书文档所有内容的方法
自动化·飞书·n8n
chinrock5 天前
如何抓包飞书小程序-飞书开发者工具
小程序·飞书
WYiQIU5 天前
11月面了7.8家前端岗,兄弟们12月我先躺为敬...
前端·vue.js·react.js·面试·前端框架·飞书
2022.11.7始学前端6 天前
第十八课 小红书笔记 + 自动创建的飞书多维表
飞书·coze
玩泥巴的8 天前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书
xdpcxq102910 天前
使用.NET 8+ 与飞书API构建同步服务
数据库·.net·飞书
mudtools11 天前
使用.NET 8+ 与飞书API构建组织架构同步服务
架构·.net·飞书
zyplayer-doc11 天前
目录支持批量操作,文档增加可见范围、锁定功能,PDF查看优化,zyplayer-doc 2.5.8 发布啦!
数据库·人工智能·pdf·编辑器·飞书·石墨文档
zyplayer-doc12 天前
重写OFD查看器,完善PDF查看器,增加搜索历史记录、滚动分页、目录排序等,zyplayer-doc 2.5.7 发布啦!
pdf·编辑器·飞书·开源软件·创业创新·有道云笔记