Vue点击切换样式

2020-09-03 10:18:38 浏览数 (1)

先看效果图

这里直接是上代码

代码语言:javascript复制
<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循环取到索引

0 人点赞