custom-tab-bar.uvue 4.7 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">
10 11
          <text v-if="displayArrow" class="tab-item-icon tab-item-arrow uni-icon" :class="selectedIndex==0 ? 'tab-item-text-active' : ''">
            {{'\ue6bd'}}
H
hdx 已提交
12
          </text>
13 14 15 16 17 18 19
          <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>
20
      <view @click="onPlusClick">
21
        <image class="concave-image" mode="heightFix" src="/static/template/custom-tab-bar/concave.png"></image>
22
        <view class="btn-plus">
23
          <text class="btn-plus-text">+</text>
H
hdx 已提交
24 25
        </view>
      </view>
26 27
      <view class="tab-item" @click="onTabClick(1)">
        <view ref="tab-item2" class="tab-item-content">
H
hdx 已提交
28 29 30 31 32 33 34 35 36 37 38 39
          <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 已提交
40
  import { ComponentPublicInstance } from 'vue';
H
hdx 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  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[],
64 65 66
        selectedIndex: -1,
        displayArrow: false,
        $tabViewHeight: 0
H
hdx 已提交
67 68 69 70 71 72 73
      }
    },
    onLoad() {
      uni.$on('tabchange', this.onTabPageEvent)
    },
    onReady() {
      this.setSelectedIndex(0)
74 75 76 77
      this.$tabViewHeight = (this.$refs["tabview"] as Element).getBoundingClientRect().height
    },
    onUnload() {
      uni.$off('tabchange', this.onTabPageEvent)
H
hdx 已提交
78 79 80 81 82 83 84 85 86
    },
    methods: {
      onTabClick(index : number) {
        this.setSelectedIndex(index);
        if (index == 0) {
          (this.$refs["tab1"]! as ComponentPublicInstance).$callMethod('scrollTop', 0)
        }
      },
      onTabPageEvent(top : number) {
87 88 89 90
        // const tabItem1 = this.$refs["tab-item1"] as Element
        this.displayArrow = top > this.$tabViewHeight
        // const angle = displayIcon ? 180 : 0
        // tabItem1.style.setProperty('transform', `rotate(${angle}deg)`)
H
hdx 已提交
91 92 93 94 95 96 97 98 99
      },
      setSelectedIndex(index : number) {
        if (this.selectedIndex === index) {
          return
        }
        if (!this.tabList[index].init) {
          this.tabList[index].init = true
        }
        this.selectedIndex = index
100 101 102 103 104 105 106
      },
      onPlusClick() {
        uni.showModal({
          title: "提示",
          content: "你点击了 +",
          showCancel: false
        })
H
hdx 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
      }
    }
  }
</script>

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

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

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

  .tab-view {
    flex: 1;
  }

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

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

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

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

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

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

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

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

  .concave-image {
    height: 56px;
  }

  .btn-plus {
    position: absolute;
184
    left: 43px;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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 已提交
202
</style>