custom-tab-bar.uvue 4.4 KB
Newer Older
H
hdx 已提交
1
<template>
2 3
  <view class="tabs">
    <view ref="tabview" class="tab-view">
H
hdx 已提交
4 5 6
      <tab1 ref="tab1" v-if="tabList[0].init" v-show="selectedIndex==0"></tab1>
      <tab2 ref="tab2" v-if="tabList[1].init" v-show="selectedIndex==1"></tab2>
    </view>
7
    <view ref="tabbar" class="tab-bar">
8 9
      <view class="tab-item" @click="onTabClick(0)">
        <view ref="tab-item1" class="tab-item-content">
H
hdx 已提交
10 11
          <text v-if="displayArrow" class="tab-item-icon tab-item-arrow uni-icon"
            :class="selectedIndex==0 ? 'tab-item-text-active' : ''">
12
            {{'\ue6bd'}}
H
hdx 已提交
13
          </text>
14 15 16 17 18 19 20
          <text v-if="!displayArrow" class="tab-item-icon uni-icon"
            :class="selectedIndex==0 ? 'tab-item-text-active' : ''">{{'\ue644'}}</text>
          <text v-if="!displayArrow" class="tab-item-text" :class="selectedIndex==0 ? 'tab-item-text-active' : ''">
            首页
          </text>
        </view>
      </view>
21
      <view>
22
        <image class="concave-image" mode="heightFix" src="/static/template/custom-tab-bar/concave.png"></image>
H
hdx 已提交
23
      </view>
24 25
      <view class="tab-item" @click="onTabClick(1)">
        <view ref="tab-item2" class="tab-item-content">
H
hdx 已提交
26 27 28 29 30 31 32 33
          <text class="tab-item-icon uni-icon"
            :class="selectedIndex==1 ? 'tab-item-text-active' : ''">{{'\ue699'}}</text>
          <text class="tab-item-text" :class="selectedIndex==1 ? 'tab-item-text-active' : ''">
            我的
          </text>
        </view>
      </view>
    </view>
34 35 36
    <view class="btn-plus" @click="onPlusClick">
      <text class="btn-plus-text">+</text>
    </view>
H
hdx 已提交
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
  </view>
</template>

<script>
  import tab1 from './custom-tab-bar-tab1.uvue';
  import tab2 from './custom-tab-bar-tab2.uvue';

  type TabItem = {
    init : boolean,
    preload : boolean,
  }

  export default {
    components: {
      tab1,
      tab2
    },
    data() {
      return {
        tabList: [
          {
            init: false
          } as TabItem,
          {
            init: false
          } as TabItem,
        ] as TabItem[],
64 65 66
        selectedIndex: -1,
        displayArrow: false,
        $tabViewHeight: 0
H
hdx 已提交
67 68 69 70 71 72 73
      }
    },
    onLoad() {
      uni.$on('tabchange', this.onTabPageEvent)
    },
    onReady() {
      this.setSelectedIndex(0)
74 75 76 77
      this.$tabViewHeight = (this.$refs["tabview"] as Element).getBoundingClientRect().height
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
78 79 80 81 82 83 84 85 86
    },
    methods: {
      onTabClick(index : number) {
        this.setSelectedIndex(index);
        if (index == 0) {
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
      },
      onTabPageEvent(top : number) {
87
        this.displayArrow = top > this.$tabViewHeight
H
hdx 已提交
88 89 90 91 92 93 94 95 96
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
97 98 99 100 101 102 103
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
      }
    }
  }
</script>

<style>
  @font-face {
    font-family: "UniIcon";
    src: url('@/static/fonts/uni-icon.ttf');
  }

  .uni-icon {
    font-family: "UniIcon";
117
    font-size: 16px;
H
hdx 已提交
118 119 120
    font-style: normal;
  }

121
  .tabs {
H
hdx 已提交
122
    flex: 1;
123 124
    background-color: #fff;
    overflow: visible;
H
hdx 已提交
125 126 127 128 129 130 131 132 133
  }

  .tab-view {
    flex: 1;
  }

  .tab-bar {
    flex-direction: row;
    height: 56px;
134
    overflow: visible;
H
hdx 已提交
135 136 137 138
  }

  .tab-item {
    flex: 1;
139 140 141
    position: relative;
    background-color: #1e90ff;
    overflow: visible;
H
hdx 已提交
142 143 144 145 146 147 148 149
  }

  .tab-item-content {
    margin: auto;
    transition: transform 0.3s;
  }

  .tab-item-icon {
150
    color: #ccc;
H
hdx 已提交
151 152
    font-size: 12px;
    text-align: center;
153
    margin-bottom: 4px;
H
hdx 已提交
154 155 156
  }

  .tab-item-text {
157
    color: #ccc;
H
hdx 已提交
158 159 160 161 162
    font-size: 12px;
    text-align: center;
  }

  .tab-item-text-active {
163
    color: #fff;
H
hdx 已提交
164 165
  }

166
  .tab-item-arrow {
H
hdx 已提交
167 168 169
    font-size: 30px !important;
    font-weight: bold;
  }
170 171 172 173 174 175 176 177 178

  .concave-image {
    height: 56px;
  }

  .btn-plus {
    position: absolute;
    width: 70px;
    height: 70px;
179
    bottom: 21px;
180 181 182
    border-radius: 50px;
    background-color: #FE5722;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
183
    align-self: center;
184 185 186 187 188 189 190 191 192
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

  .btn-plus-text {
    color: #fff;
    font-size: 32px;
  }
193
</style>