• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
  • 相关阅读:
    L64.linux命令每日一练 -- 第十章 Linux网络管理命令 -- ifconfig和ifup
    Docker ~ 从入门到入坑。
    UNDO自动管理和手工管理、闪回操作
    34. 在排序数组中查找元素的第一个和最后一个位置
    华为ssl vpn配置案例
    【Python】numpy矩阵运算大全
    ThingsBoard 实现设备认领
    word2vec中的skip-gram--学习笔记
    HTTP协议简单理解
    用Python操控Minecraft我的世界 1.安装Spigot服务器并添加RaspberryJuice插件
  • 原文地址:https://blog.csdn.net/u014722196/article/details/137970758