vue2 在循环里,给字体加上随机颜色并加上随机图标且少重复

在循环里,给字体加上随机颜色并加上随机图标且少重复

<template>
	<div class="pbfb5">
		<el-row :gutter="32">
			<el-col :xs="6" :sm="6" :lg="6" style="margin-bottom:32px;" v-for="(item,index) in costTypeList" :key="index">
				<div class="card"  >
					<p class="title">{{item.name}}</p>
					<svg-icon :icon-class="getRandomIcon()"  class-name='cost-class' :style="{color:getRandomBlueColor()}"/>
				</div>
			</el-col>
		</el-row>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				// 遮罩层
				loading: true,
				usedIcons:[],//跟踪已使用图标
				iconClassList:['bx1','bx2','bx3','bx4','bx5','bx6','bx7','bx8','bx9','bx10','bx11','bx12','bx13'],
				costTypeList:[{id:1,name:'差旅费'},{id:2,name:'技术支持费'},{id:3,name:'餐补费'},
				{id:4,name:'采买费'},{id:5,name:'外出费'},{id:6,name:'请客费'},{id:7,name:'额外补贴费'}]
			}
		},
		created() {
		},
		methods: {
			// 生成以蓝色为主的随机颜色
			    getRandomBlueColor() {
			      return `rgb(${Math.floor(Math.random() * 100) + 100}, ${Math.floor(Math.random() * 50) + 100}, ${Math.floor(Math.random() * 150) + 100})`;
			    },
			      // 随机图标类名 图标少重复
			      getRandomIcon() {
			          // 如果所有图标都已使用,重置usedIcons数组
			            if (this.usedIcons.length === this.iconClassList.length) {
			              this.usedIcons = []; // 或者使用 this.usedIcons = this.iconClassList.slice(); 来复制数组
			            }
			        
			            // 从未使用的图标中随机选择一个
			            const unusedIcons = this.iconClassList.filter(icon => !this.usedIcons.includes(icon));
			            const randomIconIndex = Math.floor(Math.random() * unusedIcons.length);
			            const randomIcon = unusedIcons[randomIconIndex];
			        
			            // 将选择的图标添加到已使用的图标数组中
			            this.usedIcons.push(randomIcon);
			        
			            return randomIcon;
			          },
		},
	}
</script>

<style scoped>
	.card {
		background: #f2fbfb;
		display: flex;
		position: relative;
		flex-direction: column;
		justify-content: space-between;
		padding: 10%;
		cursor: pointer;
		.title{color:#333;font-size: 1.1rem;}
	}
	
	.card svg {
		position: absolute;
		right: 16px;
		top: 50%;
		margin-top: -40px;
		height: 80px;
		width: 80px;
		transition: all 0.5s ease-in-out;
	}
	
	.card:hover svg {
		transform: scale(1.2);
	}
	.cost-class{color:#333;}
</style>

相关推荐

  1. vue组件上传图片的时候图片加上水印

    2024-04-23 15:58:02       16 阅读
  2. HOW - 实现加权随机函数

    2024-04-23 15:58:02       13 阅读
  3. 加权随机负载均衡算法golang实现

    2024-04-23 15:58:02       16 阅读

最近更新

  1. Kafka发送对象消息

    2024-04-23 15:58:02       0 阅读
  2. 【C++】Google Test(gtest)单元测试

    2024-04-23 15:58:02       0 阅读
  3. 中国在生成式人工智能专利方面处于领先地位

    2024-04-23 15:58:02       1 阅读
  4. Perl中的文件系统守卫:实现自定义访问控制

    2024-04-23 15:58:02       1 阅读
  5. wpf 自定义 一个事件聚合自定义示例

    2024-04-23 15:58:02       1 阅读
  6. socketserver

    2024-04-23 15:58:02       1 阅读

热门阅读

  1. PySide6之QEasingCurve.Type

    2024-04-23 15:58:02       15 阅读
  2. Swift常用的第三方库

    2024-04-23 15:58:02       16 阅读
  3. 工作后的自我介绍

    2024-04-23 15:58:02       14 阅读
  4. ATFX:注册邀请码怎么弄?

    2024-04-23 15:58:02       18 阅读
  5. 大数据——Scala 模式匹配

    2024-04-23 15:58:02       12 阅读
  6. 第4章:GO的错误处理机制

    2024-04-23 15:58:02       12 阅读
  7. 在 C 中打印字符串 - 如何在 C 中打印字符串

    2024-04-23 15:58:02       17 阅读
  8. Postgresql数据库高级sql总结3

    2024-04-23 15:58:02       12 阅读