v-for循环出不同的背景色或图片

使用场景

通常v-for循环出来的数据都是接口给到的,但是不会有iocn图标,或者设计稿的不同背景色,因此返回数据不同就进行不同颜色或图片变更。

思路

重点是通过v-bind进行绑定class,设置它的[index],在data里面进行定义,样式里面进行引入需要的背景图或者color样式,如果是背景图也同背景色写法。

<div
        class="icon"
        :class="color[index]"
      ></div>
data(){
	return{
		color: ["colorOne", "colorTwo", "colorThere"],
	}
}

全部代码

<template>
  <div class="demo">
    <div
      class="box"
      v-for="(item,index) in list"
      :key="index"
    >
      <div
        class="icon"
        :class="color[index]"
      ></div>
      <div>{{item.name}}:{{item.age}}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      color: ["colorOne", "colorTwo", "colorThere"],
      list: [
        { name: "小红", age: "18" },
        { name: "小白", age: "22" },
        { name: "大胖", age: "31" },
      ],
    };
  },
};
</script>

<style lang="scss" scoped>
.demo {
  width: 500px;
  height: 200px;
  border: 1px solid red;
  display: flex;
  justify-content: space-around;
  .box {
    display: flex;
    
    .icon {
      width: 50px;
      height: 50px;
    }
    .colorOne {
      background: red;
    }
    .colorTwo {
      background: blue;
    }
    .colorThere {
      background: skyblue;
    }
  }
}
</style>

效果图

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值