custom-tab-bar.uvue 4.5 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>
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
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
  </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: [
          {
58 59
            init: false,
            preload: false
H
hdx 已提交
60 61
          } as TabItem,
          {
62 63
            init: false,
            preload: false
H
hdx 已提交
64 65
          } as TabItem,
        ] as TabItem[],
66 67
        selectedIndex: -1,
        displayArrow: false,
雪洛's avatar
雪洛 已提交
68
        tabViewHeight: 0
H
hdx 已提交
69 70 71 72 73 74 75
      }
    },
    onLoad() {
      uni.$on('tabchange', this.onTabPageEvent)
    },
    onReady() {
      this.setSelectedIndex(0)
DCloud-yyl's avatar
DCloud-yyl 已提交
76
      this.tabViewHeight = (this.$refs["tabview"] as UniElement).getBoundingClientRect().height
77 78 79
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
80 81 82
    },
    methods: {
      onTabClick(index : number) {
83 84
        if (this.selectedIndex == index && index == 0) {
          this.displayArrow = false;
H
hdx 已提交
85 86
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
87
        this.setSelectedIndex(index);
H
hdx 已提交
88 89
      },
      onTabPageEvent(top : number) {
雪洛's avatar
雪洛 已提交
90
        this.displayArrow = top > this.tabViewHeight
H
hdx 已提交
91 92 93 94 95 96 97 98 99
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
100 101 102 103 104 105 106
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
      }
    }
  }
</script>

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

  .uni-icon {
    font-family: "UniIcon";
120
    font-size: 16px;
H
hdx 已提交
121 122 123
    font-style: normal;
  }

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

  .tab-view {
    flex: 1;
  }

  .tab-bar {
    flex-direction: row;
    height: 56px;
137
    overflow: visible;
H
hdx 已提交
138 139 140 141
  }

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

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

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

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

  .tab-item-text-active {
166
    color: #fff;
H
hdx 已提交
167 168
  }

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

  .concave-image {
    height: 56px;
  }

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

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