在v-bind:class上绑定索引函数
<div v-for="(shop,index) in shoplist" style="max-width: 20rem;" v-bind:class="calculate(index)">
calculate(index) 此处必须添加index参数
data(){
return{
colorList:['primary','danger','secondary','info']
}
},
methods:{
calculate(index){
var nm = this.colorList[Math.floor(Math.random() * this.colorList.length)];
return "card mb-3 col-lg-3 border-"+nm;
}
}
本文介绍如何在Vue.js中利用v-bind:class结合循环(v-for)为元素动态分配样式类,通过一个示例展示了如何根据索引随机选择颜色主题。
535

被折叠的 条评论
为什么被折叠?



