custom-tab-bar.uvue 6.0 KB
Newer Older
H
hdx 已提交
1
<template>
2 3
  <view class="tabs">
    <view ref="tabview" class="tab-view">
4 5 6
      <!-- 注意tab的逻辑是:第一次使用v-if创建,创建之后不再使用v-if,而是设置visibility来隐藏和显示。不能设置tab的v-if为false,因为销毁再创建影响性能 -->
      <tab1 ref="tab1" class="tab-page" v-if="tabList[0].init" :style="{visibility:(selectedIndex==0?'visible':'hidden')}"></tab1>
      <tab2 ref="tab2" class="tab-page" v-if="tabList[1].init" :style="{visibility:(selectedIndex==1?'visible':'hidden')}"></tab2>
H
hdx 已提交
7
    </view>
8
    <view ref="tabbar" class="tab-bar">
9 10
      <view class="tab-item" @click="onTabClick(0)">
        <view ref="tab-item1" class="tab-item-content">
H
hdx 已提交
11 12
          <text v-if="displayArrow" class="tab-item-icon tab-item-arrow uni-icon"
            :class="selectedIndex==0 ? 'tab-item-text-active' : ''">
13
            {{'\ue6bd'}}
H
hdx 已提交
14
          </text>
15 16 17 18 19 20 21
          <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>
22
      <view>
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
23
        <image class="concave-image" mode="heightFix" src="/static/template/custom-tab-bar/concave.png"></image>
H
hdx 已提交
24
      </view>
25 26
      <view class="tab-item" @click="onTabClick(1)">
        <view ref="tab-item2" class="tab-item-content">
H
hdx 已提交
27 28 29 30 31 32 33 34
          <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>
35 36 37
    <view class="btn-plus" @click="onPlusClick">
      <text class="btn-plus-text">+</text>
    </view>
H
hdx 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  </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: [
          {
59
            init: true,
60
            preload: false
H
hdx 已提交
61 62
          } as TabItem,
          {
63 64
            init: false,
            preload: false
H
hdx 已提交
65 66
          } as TabItem,
        ] as TabItem[],
67
        selectedIndex: 0,
68
        displayArrow: false,
69
        lastTab1Top:0,
雪洛's avatar
雪洛 已提交
70
        tabViewHeight: 0
H
hdx 已提交
71 72 73
      }
    },
    onLoad() {
74
      uni.$on('tabchange', this.onTabPageEvent) //监听tab1页面发出的tabchange事件,触发到本页面的onTabPageEvent方法。为了判断tab1的scroll-view滚动距离
H
hdx 已提交
75 76
    },
    onReady() {
77
      // this.setSelectedIndex(0)
78
      (this.$refs["tabview"] as UniElement).getBoundingClientRectAsync()!.then((res: DOMRect) => {
79 80
        this.tabViewHeight = res.height
      });
81 82 83
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
84 85 86
    },
    methods: {
      onTabClick(index : number) {
87
        if (this.selectedIndex == index && index == 0 && this.displayArrow == true) { //首页当tab按钮变成向上时,点向上就滚动到顶
W
wanganxp 已提交
88
          // console.log("11");
89
          this.displayArrow = false;
H
hdx 已提交
90 91
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
92
        else if (index !=0){ //不在首页时,把箭头变成图标
W
wanganxp 已提交
93
          // console.log("22");
94 95 96
          this.displayArrow = false
        }
        else if (index == 0 && this.selectedIndex !=0){ //从其他tab切回首页时,检查是否需要把图标变箭头
W
wanganxp 已提交
97
          // console.log("33",this.lastTab1Top, this.tabViewHeight);
98 99
          this.displayArrow = this.lastTab1Top > this.tabViewHeight
        }
100
        this.setSelectedIndex(index);
W
wanganxp 已提交
101 102 103 104
        // console.log('index: ',index);
        // console.log('this.selectedIndex: ',this.selectedIndex);
        // console.log('this.displayArrow: ',this.displayArrow);
        // console.log('this.lastTab1Top: ',this.lastTab1Top);
H
hdx 已提交
105 106
      },
      onTabPageEvent(top : number) {
DCloud_Heavensoft's avatar
DCloud_Heavensoft 已提交
107
        // console.log('top: ',top);
108 109
        this.displayArrow = top > this.tabViewHeight //滚动1屏后,就把第一个tab的图标从首页变成向上箭头
        this.lastTab1Top = top
H
hdx 已提交
110 111 112 113 114 115 116 117 118
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
119 120 121 122 123 124 125
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
      }
    }
  }
</script>

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

  .uni-icon {
    font-family: "UniIcon";
139
    font-size: 16px;
H
hdx 已提交
140 141 142
    font-style: normal;
  }

143
  .tabs {
H
hdx 已提交
144
    flex: 1;
145 146
    background-color: #fff;
    overflow: visible;
H
hdx 已提交
147 148 149 150 151 152
  }

  .tab-view {
    flex: 1;
  }

153 154 155 156 157 158
  .tab-page {
    position: absolute;
    width: 100%;
    height: 100%;
  }

H
hdx 已提交
159 160 161
  .tab-bar {
    flex-direction: row;
    height: 56px;
162
    overflow: visible;
H
hdx 已提交
163 164 165 166
  }

  .tab-item {
    flex: 1;
167 168 169
    position: relative;
    background-color: #1e90ff;
    overflow: visible;
H
hdx 已提交
170 171 172 173 174 175 176 177
  }

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

  .tab-item-icon {
178
    color: #ccc;
H
hdx 已提交
179 180
    font-size: 12px;
    text-align: center;
181
    margin-bottom: 4px;
H
hdx 已提交
182 183 184
  }

  .tab-item-text {
185
    color: #ccc;
H
hdx 已提交
186 187 188 189 190
    font-size: 12px;
    text-align: center;
  }

  .tab-item-text-active {
191
    color: #fff;
H
hdx 已提交
192 193
  }

194
  .tab-item-arrow {
H
hdx 已提交
195 196 197
    font-size: 30px !important;
    font-weight: bold;
  }
198 199 200 201 202 203 204 205 206

  .concave-image {
    height: 56px;
  }

  .btn-plus {
    position: absolute;
    width: 70px;
    height: 70px;
207
    bottom: 21px;
208 209 210
    border-radius: 50px;
    background-color: #FE5722;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
211
    align-self: center;
212 213 214 215 216 217 218 219 220
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

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