custom-tab-bar.uvue 6.1 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)
DCloud-yyl's avatar
DCloud-yyl 已提交
78
      this.tabViewHeight = (this.$refs["tabview"] as UniElement).getBoundingClientRect().height
79 80 81
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
82 83 84
    },
    methods: {
      onTabClick(index : number) {
85
        if (this.selectedIndex == index && index == 0 && this.displayArrow == true) { //首页当tab按钮变成向上时,点向上就滚动到顶
W
wanganxp 已提交
86
          // console.log("11");
87
          this.displayArrow = false;
H
hdx 已提交
88
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
89
          this.lastTab1Top = 0;//Todo: 临时解决android平台scroll-view在某些情况未触发onscroll事件的bug
H
hdx 已提交
90
        }
91
        else if (index !=0){ //不在首页时,把箭头变成图标
W
wanganxp 已提交
92
          // console.log("22");
93 94 95
          this.displayArrow = false
        }
        else if (index == 0 && this.selectedIndex !=0){ //从其他tab切回首页时,检查是否需要把图标变箭头
W
wanganxp 已提交
96
          // console.log("33",this.lastTab1Top, this.tabViewHeight);
97 98
          this.displayArrow = this.lastTab1Top > this.tabViewHeight
        }
99
        this.setSelectedIndex(index);
W
wanganxp 已提交
100 101 102 103
        // console.log('index: ',index);
        // console.log('this.selectedIndex: ',this.selectedIndex);
        // console.log('this.displayArrow: ',this.displayArrow);
        // console.log('this.lastTab1Top: ',this.lastTab1Top);
H
hdx 已提交
104 105
      },
      onTabPageEvent(top : number) {
106 107 108
        // console.log('top: ',top); //iOS在v-show为false时,不应该触发这个事件
        this.displayArrow = top > this.tabViewHeight //滚动1屏后,就把第一个tab的图标从首页变成向上箭头
        this.lastTab1Top = top
H
hdx 已提交
109 110 111 112 113 114 115 116 117
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
118 119 120 121 122 123 124
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
      }
    }
  }
</script>

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

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

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

  .tab-view {
    flex: 1;
  }

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

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

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

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

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

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

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

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

  .concave-image {
    height: 56px;
  }

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

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