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
  </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)
雪洛's avatar
雪洛 已提交
76
      this.tabViewHeight = (this.$refs["tabview"] as Element).getBoundingClientRect().height
77 78 79
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
80 81 82 83 84 85 86 87 88
    },
    methods: {
      onTabClick(index : number) {
        this.setSelectedIndex(index);
        if (index == 0) {
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
      },
      onTabPageEvent(top : number) {
雪洛's avatar
雪洛 已提交
89
        this.displayArrow = top > this.tabViewHeight
H
hdx 已提交
90 91 92 93 94 95 96 97 98
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
99 100 101 102 103 104 105
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
      }
    }
  }
</script>

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

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

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

  .tab-view {
    flex: 1;
  }

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

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

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

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

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

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

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

  .concave-image {
    height: 56px;
  }

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

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