Vue 實(shí)現(xiàn)v-for循環(huán)的時(shí)候更改 class的樣式名稱
在v-bind:class上綁定索引函數(shù)
<div v-for="(shop,index) in shoplist" style="max-width: 20rem;" v-bind:class="calculate(index)">
calculate(index) 此處必須添加index參數(shù)
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; } }
補(bǔ)充知識:vue——如何給v-for循環(huán)出來的元素設(shè)置不同的樣式
例如給循環(huán)出來的四個(gè)盒子設(shè)置不同的背景色
第一步:給要循環(huán)的盒子動態(tài)綁定class名 并且傳入一個(gè)數(shù)組
<div v-for="(i,a) in serve" class="sever_box2"> <div :class="sstt[a]"> <img :src="i.imgs" alt=""/> <router-link :to="i.url"> <span>{{i.title}}</span> </router-link> <p>{{i.english}}</p> </div> </div>
第二步:在data中定義這個(gè)數(shù)組
data() { return { sstt: [ "ss1", "ss2", "ss3", "ss4", ] }
第三步:在style中分別設(shè)置顏色
.ss1{ background: #71b262; } .ss2{ background: #6288b2; } .ss3 { background: #ecac60; } .ss4{ background: #f87171; }
完成啦!
以上這篇Vue 實(shí)現(xiàn)v-for循環(huán)的時(shí)候更改 class的樣式名稱就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié)
- vue中異步函數(shù)async和await的用法說明
- vue中用 async/await 來處理異步操作
- vue如何使用async、await實(shí)現(xiàn)同步請求
- 詳解vue中async-await的使用誤區(qū)
- vue如何在for循環(huán)中設(shè)置ref并獲取$refs
- Vue中實(shí)現(xiàn)v-for循環(huán)遍歷圖片的方法
- 使用vue?v-for循環(huán)圖片路徑方式
- vue在?for?循環(huán)里使用異步調(diào)用?async/await的方法
相關(guān)文章
vue cli4.0項(xiàng)目引入typescript的方法
這篇文章主要介紹了vue cli4.0項(xiàng)目引入typescript的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07前端設(shè)置cookie之vue-cookies使用及說明
這篇文章主要介紹了前端設(shè)置cookie之vue-cookies使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue進(jìn)入頁面加載數(shù)據(jù)緩慢實(shí)現(xiàn)loading提示過程
這篇文章主要介紹了vue進(jìn)入頁面加載數(shù)據(jù)緩慢實(shí)現(xiàn)loading提示過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue 中指令v-bind動態(tài)綁定及與v-for結(jié)合使用詳解
這篇文章主要為大家介紹了Vue 中指令v-bind動態(tài)綁定及與v-for結(jié)合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09vue新手入門出現(xiàn)function () { [native code]&nbs
這篇文章主要介紹了vue新手入門出現(xiàn)function () { [native code] }錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04