亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue中動(dòng)態(tài)Class實(shí)戰(zhàn)示例

 更新時(shí)間:2023年11月21日 09:22:43   作者:醉魚(yú)  
這篇文章主要為大家介紹了Vue中動(dòng)態(tài)Class的實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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)文章

最新評(píng)論