|
| 1 | +<template> |
| 2 | + <!-- 自定义导航栏: 默认透明不可见, scroll-view 滚动到 50 时展示 --> |
| 3 | + <view class="fly-navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }"> |
| 4 | + <!-- 1/3,多于1个页面,用返回图标 --> |
| 5 | + <navigator v-if="pages.length > 1" open-type="navigateBack" class="left-icon"> |
| 6 | + <view class="bg-gray-500/80 rounded-full w-8 h-8 flex items-center justify-center"> |
| 7 | + <button class="i-carbon-chevron-left text-white w-7 h-7"></button> |
| 8 | + </view> |
| 9 | + </navigator> |
| 10 | + <!-- 2/3,只有1个页面,如果不是tabbar,需要首页图标 --> |
| 11 | + <!-- 这种情况一般出现在用户直接打开分享出去的详情页面,或者使用redirectTo等API --> |
| 12 | + <navigator |
| 13 | + v-else-if="!isTabbar" |
| 14 | + open-type="switchTab" |
| 15 | + url="/pages/index/index" |
| 16 | + class="left-icon" |
| 17 | + > |
| 18 | + <view class="bg-gray-500/80 rounded-full w-8 h-8 flex items-center justify-center"> |
| 19 | + <button class="i-carbon-home text-white w-6 h-6"></button> |
| 20 | + </view> |
| 21 | + </navigator> |
| 22 | + <!-- 3/3,如果当前页就是tabbar页,不用去首页,也就是什么图标都不需要 --> |
| 23 | + <view class="title">{{ title || '' }}</view> |
| 24 | + </view> |
| 25 | +</template> |
| 26 | + |
| 27 | +<script lang="ts" setup> |
| 28 | +import { getIsTabbar } from '@/utils/index' |
| 29 | +
|
| 30 | +defineProps<{ title?: string }>() |
| 31 | +// 获取页面栈 |
| 32 | +const pages = getCurrentPages() |
| 33 | +const isTabbar = getIsTabbar() |
| 34 | +console.log({ isTabbar, pagesLen: pages.length }) |
| 35 | +
|
| 36 | +// 获取屏幕边界到安全区域距离 |
| 37 | +const { safeAreaInsets } = uni.getSystemInfoSync() |
| 38 | +</script> |
| 39 | + |
| 40 | +<style lang="scss" scoped> |
| 41 | +.fly-navbar { |
| 42 | + position: fixed; |
| 43 | + top: 0; |
| 44 | + left: 0; |
| 45 | + z-index: 9; |
| 46 | + width: 750rpx; |
| 47 | + color: #000; |
| 48 | + background-color: transparent; |
| 49 | +
|
| 50 | + .left-icon { |
| 51 | + position: absolute; |
| 52 | + left: 0; |
| 53 | + display: flex; |
| 54 | + align-items: center; |
| 55 | + justify-content: center; |
| 56 | + width: 44px; |
| 57 | + height: 44px; |
| 58 | + font-size: 44rpx; |
| 59 | + color: #000; |
| 60 | + } |
| 61 | +
|
| 62 | + .title { |
| 63 | + display: flex; |
| 64 | + align-items: center; |
| 65 | + justify-content: center; |
| 66 | + height: 44px; |
| 67 | + font-size: 32rpx; |
| 68 | + color: transparent; |
| 69 | + } |
| 70 | +} |
| 71 | +</style> |
0 commit comments