Tab.vue 1.26 KB
<template>
  <div class="tab">
    <template v-for="(i,index) in data">
      <div
        @click="change(index)"
        :class="['item' ,'text-center', {'active':index == tabIndex}]"
        :key="index"
      >{{i}}</div>
    </template>
    <div :class="['line',`type-${tabIndex}`]"></div>
  </div>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'Tab',
  props: {
    data: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      tabIndex: 0
    }
  },
  mounted () {
  },
  methods: {
    change (tabIndex) {
      this.tabIndex = tabIndex
      this.$emit('change', tabIndex)
    }
  }
}
</script>

<style scoped lang='scss'>
.tab {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  background-color: #fff;
  height: 44px;
  .item {
    color: #a9a9a9;
    font-size: 14px;
    transition: all 0.5s;
  }
  .active {
    font-size: 16px;
    color: #2c2c2c;
    font-weight: 500;
  }
  .line {
    position: absolute;
    bottom: 0;
    background: #5850ff;
    border-radius: 2px;
    height: 3px;
    width: 40px;
    transform: translateX(-50%);
    transition: all 0.25s;
  }
  .type-0 {
    left: 25%;
  }
  .type-1 {
    left: 75%;
  }
}
</style>