vue中v-for通過動態(tài)綁定class實(shí)現(xiàn)觸發(fā)效果
vue動態(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動態(tài)綁定class,動態(tài)添加元素,動態(tài)刪除某個(gè)元素的實(shí)現(xiàn)
主要解決了在v-for時(shí),如何給每個(gè)item添加動態(tài)的樣式,即是說,鼠標(biāo)滑動到某一項(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通過動態(tài)綁定class實(shí)現(xiàn)觸發(fā)效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
基于Vue實(shí)現(xiàn)HTML轉(zhuǎn)PDF并導(dǎo)出
這篇文章主要為大家介紹了三種方法,可以實(shí)現(xiàn)將HTML頁面轉(zhuǎn)為PDF并實(shí)現(xiàn)下載。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2022-04-04
Vue2實(shí)現(xiàn)txt文件在線預(yù)覽的代碼示例
txt文件在線預(yù)覽不需要下載另外的插件,主要有兩種形式,一種是上傳完成后實(shí)現(xiàn)預(yù)覽;另一種是后端提供文件下載接口,獲取文件在線地址實(shí)現(xiàn)預(yù)覽;本文給大家介紹了Vue2實(shí)現(xiàn)txt文件在線預(yù)覽的代碼示例,需要的朋友可以參考下2025-01-01
Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間
在 Vue 開發(fā)中,利用 new Date() 方法可以方便地獲取當(dāng)前時(shí)間,并通過 Date 對象的方法進(jìn)行時(shí)間格式化和操作。通過本文的介紹,您應(yīng)該對在 Vue 中獲取當(dāng)前時(shí)間有了更深入的了解,并了解了一些常見的時(shí)間操作方法,需要的朋友可以參考下2023-07-07
關(guān)于vant的日歷組件,在iPhonex上可選日期空白
這篇文章主要介紹了關(guān)于vant的日歷組件,在iPhonex上可選日期空白,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
VUE子組件向父組件傳值詳解(含傳多值及添加額外參數(shù)場景)
這篇文章主要給大家介紹了關(guān)于VUE子組件向父組件傳值(含傳多值及添加額外參數(shù)場景)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
解決vue做詳情頁跳轉(zhuǎn)的時(shí)候使用created方法 數(shù)據(jù)不會更新問題
這篇文章主要介紹了解決vue做詳情頁跳轉(zhuǎn)的時(shí)候使用created方法 數(shù)據(jù)不會更新問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
淺談Vue使用Cascader級聯(lián)選擇器數(shù)據(jù)回顯中的坑
這篇文章主要介紹了淺談Vue使用Cascader級聯(lián)選擇器數(shù)據(jù)回顯中的坑,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

