starmusic-mobile
1<template>
2 <view class="app-tab-bar">
3 <view
4 v-for="tab in tabs"
5 :key="tab.path"
6 class="tab-item tv-item d-press-sm"
7 :class="{ active: tab.path === currentPath }"
8 :data-navid="'tab-' + tab.key"
9 @click="handleSwitch(tab)"
10 >
11 <image class="tab-icon" :src="iconSrc(tab)" mode="aspectFit" />
12 <text class="tab-text">{{ tab.name }}</text>
13 </view>
14 </view>
15</template>
16
17<script>
18/**
19 * 自绘底部导航栏(液态玻璃 / Liquid Glass)
20 *
21 * 为什么自绘:原生 tabBar 的 backgroundColor 仅支持纯色,无法做半透明 + 背景模糊,
22 * 因此用 uni.hideTabBar 隐藏原生栏后由本组件渲染;页面切换仍走 uni.switchTab
23 * (tabBar 页面的官方跳转方式,隐藏原生栏后依然有效)。
24 *
25 * 液态玻璃要点(纯 CSS 近似,不依赖 SVG 置换滤镜,避免 App webview 性能风险):
26 * 1) 半透明底 + backdrop-filter 模糊,并叠加 saturate 补色(高斯模糊会发灰,需提色);
27 * 2) 边缘高光:浅色描边 + 顶部内阴影亮线,模拟玻璃边缘的折射亮边;
28 * 3) 悬浮投影:与页面内容拉开层次,产生"浮在表面"的观感;
29 * 4) 选中项渲染为内嵌的玻璃胶囊。
30 * 选中态图标复用 static/tabbar/{name}-active-{主题id}.png(由 scripts/gen-theme-tabbar.js 生成)。
31 */
32import { getThemeId, markNativeTabBarHidden } from '../common/theme.js'
33import { DEFAULT_THEME_ID } from '../common/constants.js'
34import event from '../common/event.js'
35
36const TABS = [
37 { key: 'search', name: '搜索', path: '/pages/index/index', icon: 'search' },
38 { key: 'list', name: '歌单', path: '/pages/my-list/my-list', icon: 'my' },
39 { key: 'setting', name: '设置', path: '/pages/setting/setting', icon: 'setting' },
40]
41
42/**
43 * 原生 tabBar 是否已隐藏(应用级一次即可)。
44 * 原生 tabBar 是应用级视图,每个 tab 页挂载时重复调用 hideTabBar 会触发
45 * 无意义的原生布局变更,正是切 tab 时"卡一下"的来源之一。
46 */
47let nativeHidden = false
48
49/** 切页去重窗口:这段时间内对同一目标页的重复点击直接忽略,避免多次 switchTab 叠加 */
50const SWITCH_LOCK_MS = 800
51
52/** 全部 tabBar 页面路由,用于判断读到的路由是否可信 */
53const TAB_PATHS = TABS.map((t) => t.path)
54
55export default {
56 name: 'AppTabBar',
57 data() {
58 return {
59 tabs: TABS,
60 currentPath: '',
61 themeId: getThemeId(),
62 }
63 },
64 created() {
65 /** 正在切换的目标页与时间戳(非响应式,仅用于连点去重) */
66 this._switchingPath = ''
67 this._switchingAt = 0
68 this._offTabTheme = event.on('settingUpdated', (keys) => {
69 if (!keys || keys.indexOf('common.theme') >= 0) this.themeId = getThemeId()
70 })
71 },
72 mounted() {
73 this.refreshCurrent()
74 this.hideNative()
75 // 页面每次显示都重算高亮:mounted 阶段 App 端页面栈可能还没更新到本页,
76 // 只靠 mounted 会读到上一个页面的路由(高亮停在原来的 tab,需再点一次)
77 this._offPageShow = event.on('pageShow', () => this.refreshCurrent())
78 },
79 beforeUnmount() {
80 if (this._offTabTheme) {
81 this._offTabTheme()
82 this._offTabTheme = null
83 }
84 if (this._offPageShow) {
85 this._offPageShow()
86 this._offPageShow = null
87 }
88 },
89 methods: {
90 /** 隐藏原生 tabBar(原生栏无法半透明,隐藏后由本组件接管) */
91 hideNative() {
92 if (nativeHidden) return
93 try {
94 uni.hideTabBar({
95 animation: false,
96 success: () => {
97 nativeHidden = true
98 // 通知主题模块:不要再对已隐藏的原生栏同步样式/图标,
99 // 否则原生栏会被重新拉进原生布局(切 tab 卡顿、看上去点了没反应)
100 markNativeTabBarHidden()
101 },
102 })
103 } catch (e) {
104 // ignore
105 }
106 },
107 /** 读取当前页面路由用于高亮 */
108 refreshCurrent(isRetry) {
109 try {
110 const pages = getCurrentPages()
111 const route = pages.length ? pages[pages.length - 1].route : ''
112 this.currentPath = route ? `/${route}` : ''
113 } catch (e) {
114 this.currentPath = ''
115 }
116 // 兜底重读一次:App 端页面栈偶尔晚一拍更新(新建页面在 mounted 阶段还不是栈顶),
117 // 此时立刻读到的是上一个页面,高亮就会停在原来的 tab 上。只重试一次,不会循环。
118 if (!isRetry && TAB_PATHS.indexOf(this.currentPath) < 0) {
119 setTimeout(() => this.refreshCurrent(true), 60)
120 }
121 },
122 /** 选中态图标:默认主题用原始图标,其它主题用主题色版本 */
123 iconSrc(tab) {
124 if (tab.path !== this.currentPath) return `/static/tabbar/${tab.icon}.png`
125 const suffix = this.themeId === DEFAULT_THEME_ID ? '' : `-${this.themeId}`
126 return `/static/tabbar/${tab.icon}-active${suffix}.png`
127 },
128 handleSwitch(tab) {
129 if (tab.path === this.currentPath) return
130 // 同一目标的重复点击直接忽略:首次切入某个 tab 页要新建 WebView,耗时较长,
131 // 用户容易连点;重复 switchTab 只会让切换更慢更乱
132 const now = Date.now()
133 if (tab.path === this._switchingPath && now - this._switchingAt < SWITCH_LOCK_MS) return
134 this._switchingPath = tab.path
135 this._switchingAt = now
136 // 先高亮目标项:原生切换有延迟,立刻给出反馈,避免"点了没反应"的错觉
137 this.currentPath = tab.path
138 uni.switchTab({
139 url: tab.path,
140 fail: () => {
141 // 切换失败时回退高亮,保证界面与实际页面一致
142 this._switchingPath = ''
143 this.refreshCurrent()
144 },
145 })
146 },
147 },
148}
149</script>
150
151<style lang="scss" scoped>
152.app-tab-bar {
153 display: flex;
154 align-items: stretch;
155 margin: 6rpx 16rpx calc(env(safe-area-inset-bottom) + 10rpx);
156 padding: 8rpx;
157 border-radius: 34rpx;
158 /* ① 半透明底 + 玻璃模糊 + 饱和补偿:仅背景图开启时生效(变量由 background.js 注入),否则 none 跳过合成 */
159 background: rgba(255, 255, 255, 0.55);
160 backdrop-filter: var(--glass-bar, none);
161 -webkit-backdrop-filter: var(--glass-bar, none);
162 /* ② 边缘高光(描边 + 内侧亮线) ③ 悬浮投影 */
163 border: 1rpx solid rgba(255, 255, 255, 0.65);
164 box-shadow:
165 inset 0 1rpx 0 rgba(255, 255, 255, 0.95),
166 inset 0 -1rpx 0 rgba(255, 255, 255, 0.35),
167 0 8rpx 28rpx rgba(0, 0, 0, 0.12);
168}
169
170.tab-item {
171 flex: 1;
172 display: flex;
173 flex-direction: column;
174 align-items: center;
175 justify-content: center;
176 padding: 10rpx 0 8rpx;
177 border-radius: 26rpx;
178 transition: background 0.2s ease, box-shadow 0.2s ease;
179}
180
181/* ④ 选中项:内嵌玻璃胶囊 */
182.tab-item.active {
183 background: rgba(255, 255, 255, 0.6);
184 box-shadow:
185 inset 0 1rpx 0 rgba(255, 255, 255, 0.95),
186 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
187}
188
189.tab-icon {
190 width: 44rpx;
191 height: 44rpx;
192}
193
194.tab-text {
195 margin-top: 4rpx;
196 font-size: 20rpx;
197 line-height: 1.2;
198 color: var(--text-muted, #909399);
199}
200
201.tab-item.active .tab-text {
202 color: var(--theme-color, #31c27c);
203 font-weight: 600;
204}
205</style>