all files / src/tabs/ tabs.vue

0% Statements 0/18
0% Branches 0/10
0% Functions 0/1
0% Lines 0/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                                       
<template>
  <div class="mu-tabs">
    <slot></slot>
    <span class="mu-tab-link-highlight" ref="highlight" :class="lineClass"></span>
  </div>
</template>
 
<script>
export default {
  name: 'mu-tabs',
  props: {
    lineClass: {
      type: [String, Object, Array]
    },
    value: {}
  },
  data () {
    return {
      tabLightStyle: {
        width: '100%',
        transform: 'translate3d(0, 0, 0)'
      }
    }
  },
  updated () {
    this.setTabLightStyle()
  },
  methods: {
    handleTabClick (value, tab) {
      if (this.value !== value) {
        this.$emit('change', value)
      }
    },
    getActiveIndex () {
      if (!this.$children || this.$children.length === 0) return -1
      let activeIndex = -1
      this.$children.forEach((tab, i) => {
        if (tab.value === this.value) {
          activeIndex = i
          return false
        }
      })
      return activeIndex
    },
    setTabLightStyle () {
      const x = this.getActiveIndex() * 100
      const len = this.$children.length
      const el = this.$refs.highlight
      el.style.width = len > 0 ? (100 / len).toFixed(4) + '%' : '100%'
      el.style.transform = 'translate3d(' + x + '%, 0, 0)'
    }
  },
  mounted () {
    this.setTabLightStyle()
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-tabs{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: @primaryColor;
  color: #FFF;
  text-align: center;
  position: relative;
  z-index: 100;
  width: 100%;
}
 
.mu-tab-link-highlight{
  position: absolute;
  left: 0;
  bottom: 0;
  height: 2px;
  background-color: @accentColor;
  transition: transform .3s;
  backface-visibility: hidden;
}
 
</style>