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