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 @click="onPlusClick">
22
        <image class="concave-image" mode="heightFix" src="/static/template/custom-tab-bar/concave.png"></image>
23
        <view class="btn-plus">
24
          <text class="btn-plus-text">+</text>
H
hdx 已提交
25 26
        </view>
      </view>
27 28
      <view class="tab-item" @click="onTabClick(1)">
        <view ref="tab-item2" class="tab-item-content">
H
hdx 已提交
29 30 31 32 33 34 35 36 37 38 39 40
          <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>
  </view>
</template>

<script>
H
hdx 已提交
41
  import { ComponentPublicInstance } from 'vue';
H
hdx 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  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[],
65 66 67
        selectedIndex: -1,
        displayArrow: false,
        $tabViewHeight: 0
H
hdx 已提交
68 69 70 71 72 73 74
      }
    },
    onLoad() {
      uni.$on('tabchange', this.onTabPageEvent)
    },
    onReady() {
      this.setSelectedIndex(0)
75 76 77 78
      this.$tabViewHeight = (this.$refs["tabview"] as Element).getBoundingClientRect().height
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
79 80 81 82 83 84 85 86 87
    },
    methods: {
      onTabClick(index : number) {
        this.setSelectedIndex(index);
        if (index == 0) {
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
      },
      onTabPageEvent(top : number) {
88
        this.displayArrow = top > this.$tabViewHeight
H
hdx 已提交
89 90 91 92 93 94 95 96 97
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
98 99 100 101 102 103 104
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
      }
    }
  }
</script>

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

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

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

  .tab-view {
    flex: 1;
  }

  .tab-view-item {
    flex: 1;
  }

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

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

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

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

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

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

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

  .concave-image {
    height: 56px;
  }

  .btn-plus {
    position: absolute;
182
    left: 43px;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    width: 70px;
    height: 70px;
    border-radius: 50px;
    background-color: #FE5722;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
    /* margin-left: auto;
    margin-right: auto; */
    margin-top: -35px;
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

  .btn-plus-text {
    color: #fff;
    font-size: 32px;
  }
H
hdx 已提交
200
</style>