Vue点击切换样式

时间:2022-07-24
本文章向大家介绍Vue点击切换样式,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

先看效果图

这里直接是上代码

<template>
  <ul>
    <li :class="[index == status  ? 'bechoice' : '']" v-for="(item,index) in list" :key="index" @click="choice(index)">
        {{item.text}}
    </li>
  </ul>
</template>

<script>
export default {
  name: "Recharge",
  data() {
    return {
      status: 0,
      list: [
        { text: "热点" },
        { text: "政治" },
        { text: "娱乐" },
        { text: "新闻" },
        { text: "军事" },
        { text: "体育" }
      ]
    };
  },
  methods: {
    choice(index) {
      this.status = index;
    }
  }
};
</script>

<style scoped>
ul {
  list-style: none;
  display: flex;
  justify-content: space-around;
}
li {
  width: 200px;
  height: 30px;
  background: #eeeeee;
  color: #000;
  margin-right: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 5px;
}
.bechoice {
  background: red;
  color: #eeeeee;
}
</style>

注意事项:这里必须要用for循环取到索引