tab-button.vue 2.5 KB
Newer Older
Nature's avatar
Nature committed
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
<template>
  <section
    :class="[buttonClass,cusClass,showActivated]"
    class="tab-content"
    @contextmenu="preventDefault"
    @touchstart="start"
    @touchend="end"
    @touchcancel="end"
    @mousedown="start"
    @mouseup="end"
    @mouseleave="end">
    <section class="bottom-tab-button">
      <slot/>
    </section>
    <div v-show="showDivider" class="bottom-tab-divider"/>
  </section>
</template>

<script>
export default {
  name: 'TabButton',
  props: {
    cusClass: {
      type: String,
      default: '',
    },
    disable: {
      type: Boolean,
      default: false,
    },
  },
  data () {
    return {
      showActivated: '',
    }
  },
  computed: {
    showDivider () {
      return this.$parent.showDivider
    },
    buttonClass () {
      if (this.disable) return 'disable'
    },
  },
  methods: {
    preventDefault (e) {
      // 移动浏览器中长按元素会触发显示菜单,导致touchend事件不会触发,需要阻止该行为
      e.preventDefault()
    },
    start (e) {
      if (this.disable) {
        e.preventDefault()
        e.stopPropagation()
      } else {
        this.showActivated = 'activated'
      }
    },
    end () {
      // 添加一点延时,不然透明度变化太快,效果不明显
      setTimeout(() => {
        this.showActivated = ''
      }, 100)
    },
  },
}
</script>

<style lang="less">

  .h-bottom-tab {
    .tab-content {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      &.activated {
        opacity: 0.8;
      }
      .bottom-tab-button {
        width: 100%;
        height: 100%;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;
        -webkit-align-items: center;
        -webkit-justify-content: center;
        font-size: @font-size-big;

        img {
          margin-right: 10px;
        }
      }
      .bottom-tab-divider {
        width: 1px; /*no*/
        height: 50%;
        background-color: rgb(214, 217, 218);
      }
      &:last-child {
        .bottom-tab-divider {
          width: 0px;
        }
      }
      &.disable {
        color: rgba(0, 0, 0, .3) !important;
        background-color: #e6e6e6 !important;
        box-shadow: none !important;
        cursor: not-allowed !important;
        div {
          color: rgba(0, 0, 0, .3) !important;
          background-color: #e6e6e6 !important;
          box-shadow: none !important;
          cursor: not-allowed !important;
        }
      }
    }
  }

</style>