vue 动态绑定class给tabbar增加选中的状态

在这里插入图片描述

<template>
  <div class="tabctrl">
    <div
      @click="currentChange(index)"
      class="ctrl-item"
      :class="{active:index===currentIndex}"
      v-for="(item,index) in titleList"
      :key="index"
    >
      <span>{{item}}span>
    div>
  div>
template>

<script>
export default {
  name: "",
  props: {
    titleList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      currentIndex: 0,
    };
  },
  methods: {
    currentChange(index) {
      this.currentIndex = index;
    },
  },

  watch: {},
};
script>
<style lang='scss' scoped>
.tabctrl {
  position: sticky;
  top: 0;
  width: 100%;
  display: flex;
  text-align: center;
  line-height: 44px;
  height: 44px;
  background-color: #eee;
  .active span {
    color: pink;
    border-bottom: 5px solid pink;
    padding-bottom: 5px;
  }

  .ctrl-item {
    flex: 1;
  }
}
style>

你可能感兴趣的:(Vue)