1. 程式人生 > >vue寫一個簡單的頭部導航樣式切換

vue寫一個簡單的頭部導航樣式切換

<template>
  <div class="tab-head">
    <ul class="tab-list">
      <li v-for="(item,index)  in tabList" v-on:click="addClass(index)" v-bind:class="{ active:index==current}">{{item.title}}</li>
    </ul>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tabList: [
        { id: 0, title: "首頁1" },
        { id: 1, title: "首頁2" },
        { id: 2, title: "首頁3" }
      ],
      current:0
    };
  },
  methods: {
    addClass: function(index) {
      this.current = index;
    }
  }
};
</script>

style樣式:

.tab-list{
  width: 100%;
  height: 45px;
  background: rgb(84, 92, 100);
  position: absolute;
  top: 0;
  left: 0;
}
.tab-list li{
  float: left;
  cursor: pointer;
  height: 41px;
  line-height: 45px;
  padding: 0 30px;
  color: #fff;
}
.tab-list li:hover{
  color: rgb(255, 208, 75);
  background: rgb(64, 74, 80);
  border-bottom: 4px solid rgb(64, 74, 80);
}
.tab-list li.active{
  border-bottom: 4px solid rgb(255, 208, 75);
}
.active{
  color: rgb(255, 208, 75);
  background: rgb(64, 74, 80)
}

用到的知識點,點選當前元素新增樣式,點選其他元素移除兄弟元素的樣式