Vue實現active點擊切換樣式


1.html

  <div class="filter-nav" v-for="(item,index)  in  filterData.navTab" :key="index" :class="{'active':currentSort==index}" @click="active(index)">
          <span>{{item.name}}</span>
          <i v-if="item.icon" :class="'fa fa-'+item.icon"></i>
        </div>

2.在循環的代碼那里添加 

 :class="{'active':currentSort==index}"

3.添加樣式

.active {
    color: #009eef;
}

4.初始化數據

 data() {
    return {
        currentSort:0,
    }
  },

5.調用方法

 methods: {
    active(index) {
       this.currentSort  =  index;
    }
  },

當我們觸發點擊事件的時候,傳入循環的index  ,這樣我們就可以將綁定房東樣式賦給點擊到的x 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM