Vue中動(dòng)態(tài)Class實(shí)戰(zhàn)示例
需求
想實(shí)現(xiàn)一個(gè)假如有5個(gè)div塊,默認(rèn)都是灰色,鼠標(biāo)懸浮到哪個(gè)div上,那個(gè)div就顯示為黑色。
具體的實(shí)現(xiàn)業(yè)務(wù)邏輯可根據(jù)這個(gè)進(jìn)行演變
設(shè)計(jì)
通過(guò)動(dòng)態(tài) class 類名來(lái)實(shí)現(xiàn),實(shí)現(xiàn)鼠標(biāo)懸浮到div時(shí)動(dòng)態(tài)綁定class
版本
- Vue 3.3.4
- Node 20.9.0
代碼
<template>
<div class="container">
<div v-for="(box, index) in boxes" :key="index" :class="'box'+ index"
:style="{ color: box.color, backgroundColor: box.backgroundColor }">
{{ box.content }}
</div>
</div>
</template>
<script>
export default {
data() {
return {
boxes: [
{ content: 'Box 1', color: 'white', backgroundColor: 'grey' },
{ content: 'Box 2', color: 'white', backgroundColor: 'grey' },
{ content: 'Box 3', color: 'white', backgroundColor: 'grey' },
{ content: 'Box 4', color: 'white', backgroundColor: 'grey' },
{ content: 'Box 5', color: 'white', backgroundColor: 'grey' }
]
};
},
methods: {
handleMouseOver(index) {
console.log('鼠標(biāo)移入:',index)
this.boxes[index].backgroundColor = 'black';
this.boxes[index].color = 'white';
},
handleMouseOut(index) {
console.log('鼠標(biāo)移出:',index)
this.boxes[index].backgroundColor = 'grey';
this.boxes[index].color = 'white';
}
},
mounted() {
this.boxes.forEach((box, index) => {
console.log("頁(yè)面初始化:",box,index)
this.$el.querySelector('.box'+index).addEventListener('mouseover', () => this.handleMouseOver(index));
this.$el.querySelector('.box'+index).addEventListener('mouseout', () => this.handleMouseOut(index));
});
}
};
</script>以上就是Vue中動(dòng)態(tài)Class實(shí)戰(zhàn)示例的詳細(xì)內(nèi)容,更多關(guān)于Vue動(dòng)態(tài)Class的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Vue + Echarts 使用markLine標(biāo)線(precision精度問(wèn)題)
這篇文章主要介紹了解決Vue + Echarts 使用markLine標(biāo)線(precision精度問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue中router.beforeEach()的簡(jiǎn)單用法舉例
router.beforeEach()一般用來(lái)做一些進(jìn)入頁(yè)面的限制,比如沒(méi)有登錄,就不能進(jìn)入某些頁(yè)面,只有登錄了之后才有權(quán)限查看某些頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于vue中router.beforeEach()的簡(jiǎn)單用法舉例,需要的朋友可以參考下2023-01-01
vue+elementUI實(shí)現(xiàn)點(diǎn)擊按鈕互斥效果
這篇文章主要為大家詳細(xì)介紹了vue+elementUI實(shí)現(xiàn)點(diǎn)擊按鈕互斥效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
解決在vue項(xiàng)目中webpack打包后字體不生效的問(wèn)題
今天小編就為大家分享一篇解決在vue項(xiàng)目中webpack打包后字體不生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue3中進(jìn)行頁(yè)面局部刷新組件刷新的操作方法
這篇文章主要介紹了Vue3中進(jìn)行頁(yè)面局部刷新組件刷新的操作方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
vue 解決addRoutes多次添加路由重復(fù)的操作
這篇文章主要介紹了vue 解決addRoutes多次添加路由重復(fù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑)
這篇文章主要介紹了vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

