index.vue
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<template>
<view class="tab-block">
<view class='tab-list flex flex-center' :class="showMiddleButton === true?'tab-list-middle':'tab-list-default'">
<view v-for="(item, index) in tabList"
:class="'list-item flex flex-column flex-middle ' + item.middleClass"
@tap="handlePush(item, index)"
:key="index">
<view class="item-img-box">
<image
class="item-img"
:src="currentTabIndex == index ? item.selectedIconPath : item.iconPath"/>
</view>
<view class="item-text font-20 color-black" :class="{ specialColor: currentTabIndex == index}">
{{item.text}}
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
// 当前选中的tab项,如果不传则自动根据当前页面路径计算
tabIndex: {
type: Number,
default: null
}
},
data() {
return {
tabList: [
{
iconPath:"/static/tabBar/icon2.png",
selectedIconPath: "/static/tabBar/icon1.png",
text: '首页',
pagePath: '/pages/home/home',
middleClass: ''
},
{
iconPath:"/static/tabBar/icon2.png",
selectedIconPath: "/static/tabBar/icon1.png",
text: '日志',
pagePath: '/pages/home/home',
middleClass: ''
},
{
iconPath: "/static/tabBar/icon4.png",
selectedIconPath: "/static/tabBar/icon3.png",
text: '数据中心',
pagePath: '/pages/information/information',
middleClass: ''
},
{
iconPath: "/static/tabBar/icon6.png",
selectedIconPath: "/static/tabBar/icon5.png",
text: '我的',
pagePath: '/pages/me/me',
middleClass: ''
}
],
showMiddleButton: false,
};
},
computed: {
// 自动计算当前选中的tab索引
currentTabIndex() {
// 如果外部传入了 tabIndex,优先使用外部传入的值
if (this.tabIndex !== null && this.tabIndex !== undefined) {
return this.tabIndex;
}
// 否则根据当前页面路径自动计算
const pages = getCurrentPages();
if (pages.length === 0) return 0;
const currentPage = pages[pages.length - 1];
const currentPath = '/' + currentPage.route;
// 查找匹配的tab项
const matchedIndex = this.tabList.findIndex(item => {
return currentPath === item.pagePath || currentPath.startsWith(item.pagePath);
});
return matchedIndex >= 0 ? matchedIndex : 0;
}
},
methods: {
// 点击按钮
handlePush(item, index) {
if (this.currentTabIndex !== index) {
uni.reLaunch({
url: item.pagePath
})
}
}
}
}
</script>
<style lang="scss">
.specialColor{
color:#009b4d;
}
.flex {
display: flex;
flex-flow: row wrap;
}
.flex-center {
align-items: center;
justify-content: center;
}
.flex-column {
flex-direction: column;
}
.flex-middle {
align-items: center;
}
.font-20 {
font-size: 22rpx;
}
.tab-block {
position: fixed;
bottom: 32rpx;
left: 50%;
transform: translateX(-50%);
z-index: 999;
width: calc(100% - 48rpx);
// max-width: 680rpx;
padding: 0 24rpx;
box-sizing: border-box;
.tab-list{
height: 120rpx;
}
.tab-list-default{
background-color: rgba(255, 255, 255, 0.85);
border-radius: 28rpx;
box-shadow: 0rpx 4rpx 20rpx rgba(0, 0, 0, 0.1);
backdrop-filter: blur(10rpx);
}
.tab-list-middle {
position: relative;
background: url('https://res.paquapp.com/popmartvip/home/nav_bar_bg_2x.png') no-repeat center center;
background-size: cover;
}
.list-item {
flex: 1;
.item-img-box {
width: 40rpx;
height: 40rpx;
margin-bottom: 6rpx;
position: relative;
}
.item-img {
width: 40rpx;
height: 40rpx;
}
}
.mid-button {
position: relative;
.item-img-box {
width: 106rpx;
height: 106rpx;
margin-bottom: 9rpx;
position: absolute;
z-index: 1002;
top: -104rpx;
}
.item-img {
width: 106rpx;
height: 106rpx;
}
.item-text {
font-size: 20rpx;
position: absolute;
z-index: 1002;
bottom: -40rpx;
}
}
}
</style>