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)
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 89
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
90
        else if (index !=0){ //不在首页时,把箭头变成图标
W
wanganxp 已提交
91
          // console.log("22");
92 93 94
          this.displayArrow = false
        }
        else if (index == 0 && this.selectedIndex !=0){ //从其他tab切回首页时,检查是否需要把图标变箭头
W
wanganxp 已提交
95
          // console.log("33",this.lastTab1Top, this.tabViewHeight);
96 97
          this.displayArrow = this.lastTab1Top > this.tabViewHeight
        }
98
        this.setSelectedIndex(index);
W
wanganxp 已提交
99 100 101 102
        // console.log('index: ',index);
        // console.log('this.selectedIndex: ',this.selectedIndex);
        // console.log('this.displayArrow: ',this.displayArrow);
        // console.log('this.lastTab1Top: ',this.lastTab1Top);
H
hdx 已提交
103 104
      },
      onTabPageEvent(top : number) {
DCloud_Heavensoft's avatar
DCloud_Heavensoft 已提交
105
        // console.log('top: ',top);
106 107
        this.displayArrow = top > this.tabViewHeight //滚动1屏后,就把第一个tab的图标从首页变成向上箭头
        this.lastTab1Top = top
H
hdx 已提交
108 109 110 111 112 113 114 115 116
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
117 118 119 120 121 122 123
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
      }
    }
  }
</script>

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

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

141
  .tabs {
H
hdx 已提交
142
    flex: 1;
143 144
    background-color: #fff;
    overflow: visible;
145 146 147
    /* #ifdef MP */
    height: 100vh;
    /* #endif */
H
hdx 已提交
148 149 150 151 152 153
  }

  .tab-view {
    flex: 1;
  }

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

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

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

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

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

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

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

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

  .concave-image {
    height: 56px;
  }

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

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