tabBar.vue 5.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<template>
  <uni-tabbar>
    <div
      :style="{backgroundColor:backgroundColor}"
      class="uni-tabbar">
      <div
        :style="{backgroundColor:borderColor}"
        class="uni-tabbar-border" />
      <div
        v-for="(item,index) in list"
        :key="item.pagePath"
        class="uni-tabbar__item"
        @click="_switchTab(item,index)">
        <div class="uni-tabbar__bd">
          <div
            v-if="item.iconPath"
17
            :class="{'uni-tabbar__icon__diff':!item.text}"
fxy060608's avatar
fxy060608 已提交
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
            class="uni-tabbar__icon">
            <img :src="_getRealPath($route.meta.pagePath===item.pagePath?item.selectedIconPath:item.iconPath)">
          </div>
          <div
            v-if="item.text"
            :style="{color:$route.meta.pagePath===item.pagePath?selectedColor:color,fontSize:item.iconPath?'10px':'14px'}"
            class="uni-tabbar__label">
            {{ item.text }}
          </div>
          <div
            v-if="item.redDot"
            :class="{'uni-tabbar__badge':!!item.badge}"
            class="uni-tabbar__reddot">{{ item.badge }}</div>
        </div>
      </div>
    </div>
    <div class="uni-placeholder" />
  </uni-tabbar>
</template>

<style>
uni-tabbar {
  display: block;
  box-sizing: border-box;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  z-index: 998;
}

uni-tabbar .uni-tabbar {
  display: flex;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  z-index: 998;
  box-sizing: border-box;
57 58 59
  padding-bottom: 0;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64
}

uni-tabbar .uni-tabbar ~ .uni-placeholder {
  width: 100%;
  height: 50px;
65 66 67
  margin-bottom: 0;
  margin-bottom: constant(safe-area-inset-bottom);
  margin-bottom: env(safe-area-inset-bottom);
fxy060608's avatar
fxy060608 已提交
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
}

uni-tabbar .uni-tabbar * {
  box-sizing: border-box;
}

uni-tabbar .uni-tabbar__item {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex: 1;
  font-size: 0;
  text-align: center;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

uni-tabbar .uni-tabbar__bd {
  position: relative;
  height: 50px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

uni-tabbar .uni-tabbar__icon {
  position: relative;
  display: inline-block;
  margin-top: 5px;
  width: 27px;
  height: 27px;
}

102 103 104 105 106 107
uni-tabbar .uni-tabbar__icon.uni-tabbar__icon__diff{
  margin-top: 0px;
  width: 34px;
  height: 34px;
}

fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
uni-tabbar .uni-tabbar__icon img {
  width: 100%;
  height: 100%;
}

uni-tabbar .uni-tabbar__label {
  position: relative;
  text-align: center;
  font-size: 10px;
  line-height: 1.8;
}

uni-tabbar .uni-tabbar-border {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

uni-tabbar .uni-tabbar__reddot {
  position: absolute;
  top: 6px;
132
  left: 16px;
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  width: 12px;
  height: 12px;
  display: inline-block;
  border-radius: 50%;
  background-color: #f43530;
  color: #ffffff;
}

uni-tabbar .uni-tabbar__badge {
  top: 4px;
  border-radius: 18px;
  min-width: 8px;
  width: auto;
  height: auto;
  padding: 0.15em 0.5em;
  font-size: 12px;
  line-height: 1.2;
  white-space: nowrap;
}
</style>

<script>
import getRealPath from 'uni-platform/helpers/get-real-path'

export default {
  name: 'TabBar',
  props: {
    position: {
      default: 'bottom',
      validator (value) {
        return ['bottom', 'top'].indexOf(value) !== -1
      }
    },
    color: {
      type: String,
      default: '#999'
    },
    selectedColor: {
      type: String,
      default: '#007aff'
    },
    backgroundColor: {
      type: String,
      default: '#f7f7fa'
    },
    borderStyle: {
      default: 'black',
      validator (value) {
        return ['black', 'white'].indexOf(value) !== -1
      }
    },
    list: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  computed: {
    borderColor () {
      return this.borderStyle === 'white'
        ? 'rgba(255, 255, 255, 0.33)'
        : 'rgba(0, 0, 0, 0.33)'
    }
  },
  watch: {
    '$route' (to, from) {
      if (to.meta.isTabBar) {
        this.__path__ = to.path
      }
    }
  },
  beforeCreate () {
    this.__path__ = this.$route.path
  },
  methods: {
    _getRealPath (filePath) {
      if (filePath.indexOf('/') !== 0) {
        filePath = '/' + filePath
      }
      return getRealPath(filePath)
    },
    _switchTab ({
      text,
      pagePath
    }, index) {
      let url = '/' + pagePath
      if (url === __uniRoutes[0].alias) {
        url = '/'
222
      }
fxy060608's avatar
fxy060608 已提交
223
      const detail = {
224 225 226
        index,
        text,
        pagePath
fxy060608's avatar
fxy060608 已提交
227 228 229 230 231 232 233 234 235 236
      }
      if (this.$route.path !== url) {
        this.__path__ = this.$route.path
        uni.switchTab({
          from: 'tabBar',
          url,
          detail
        })
      } else {
        UniServiceJSBridge.emit('onTabItemTap', detail)
fxy060608's avatar
fxy060608 已提交
237 238 239 240 241
      }
    }
  }
}
</script>