vue中v-for通過(guò)動(dòng)態(tài)綁定class實(shí)現(xiàn)觸發(fā)效果
vue動(dòng)態(tài)綁定class練習(xí)。
:class=“{ ‘類名1':條件表達(dá)式,‘類名2':條件表達(dá)式… }”
<template>
<div class="app-*">
<ul>
<li
v-for="(item,i) of list"
:key="i"
class="item"
@click="clickIndex=i"
:class="{'click':i==clickIndex}"
></li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
list: [1, 2, 3, 4],
clickIndex: -1
};
},
methods: {}
};
</script>
<style scoped>
.item {
display: inline-block;
width: 100px;
height: 100px;
cursor: pointer;
border: 1px solid black;
}
.item:hover {
background: gray;
}
.item.click {
background: red;
}
</style>
補(bǔ)充:vue之v-for中給每個(gè)item動(dòng)態(tài)綁定class,動(dòng)態(tài)添加元素,動(dòng)態(tài)刪除某個(gè)元素的實(shí)現(xiàn)
主要解決了在v-for時(shí),如何給每個(gè)item添加動(dòng)態(tài)的樣式,即是說(shuō),鼠標(biāo)滑動(dòng)到某一項(xiàng)時(shí),可以單獨(dú)改變某一項(xiàng)的樣式,同時(shí)添加按鈕等操作。以及刪除某一項(xiàng)的操作。
<template>
<div class="hello">
<ul>
<li v-for="(item, itemIndex) in test"
:key="item.id"
:class="{defaultClass: itemIndex === isActive}"
@mouseenter="onMouseEnter(itemIndex)"
@mouseleave="onMouseLeave">
{{ itemIndex+1 }} :{{ item.title }}
<button v-if="isActive === itemIndex" @click="deleteItem(itemIndex)">刪除({{itemIndex+1}})</button>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
test: [
{
id: 1,
title: 'title first'
},
{
id: 2,
title: 'title second'
},
{
id: 3,
title: 'title third'
}
],
isActive: ''
}
},
methods: {
onMouseEnter(index) {
this.isActive = index
},
onMouseLeave() {
this.isActive = ''
},
deleteItem(index) {
this.test.splice(index, 1)
}
},
computed: {
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
/* display: inline-block; */
margin:10px;
}
a {
color: #42b983;
}
.defaultClass{
background-color: red;
}
</style>
總結(jié)
以上所述是小編給大家介紹的vue中v-for通過(guò)動(dòng)態(tài)綁定class實(shí)現(xiàn)觸發(fā)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue動(dòng)態(tài)綁定多個(gè)class以及帶上三元運(yùn)算或其他條件
- Vue v-bind動(dòng)態(tài)綁定class實(shí)例方法
- vue動(dòng)態(tài)綁定class的幾種常用方式小結(jié)
- vue動(dòng)態(tài)綁定class選中當(dāng)前列表變色的方法示例
- 快速解決vue動(dòng)態(tài)綁定多個(gè)class的官方實(shí)例語(yǔ)法無(wú)效的問(wèn)題
- vue 中動(dòng)態(tài)綁定class 和 style的方法代碼詳解
- vue通過(guò)tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定
相關(guān)文章
vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼
這篇文章主要介紹了vue項(xiàng)目中仿element-ui彈框效果的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue基于v-charts封裝雙向條形圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue基于v-charts封裝雙向條形圖的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue增加強(qiáng)緩存和版本號(hào)的實(shí)現(xiàn)方法
這篇文章主要介紹了vue增加強(qiáng)緩存和版本號(hào)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

