index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <view
  3. class="navbar"
  4. :class="{'gradientBgc': !noBgColor}"
  5. :style="{
  6. 'height': (navbarHeight < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight + 10) + 'px',
  7. 'line-height': (navbarHeight < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight + 10) + 'px',
  8. }"
  9. >
  10. <view class="navbar-box" :style="{'paddingTop': statusBarHeight + 'px'}">
  11. <image
  12. v-if="showLogo"
  13. src="https://minio.menduner.com/dev/fe9890be9b1176f84f2aa282b0f6adce300b133f65eb3d7b45ae057aa5698324.png"
  14. class="navbar-box-logo"
  15. :style="{'height': defaultLogoHeight + 'px'}"
  16. ></image>
  17. <view v-else class="nav-box-title">
  18. <uni-icons type="left" size="25" class="ss-m-l-20" color="#0E100F" @tap="goBack"></uni-icons>
  19. <view class="title-text" :class="[{'text-left': textLeft}, {'text-center': !textLeft}]">{{ title }}</view>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script setup>
  25. import { ref } from 'vue'
  26. import { onLoad } from '@dcloudio/uni-app'
  27. defineProps({
  28. title: {
  29. type: String,
  30. default: '门墩儿'
  31. },
  32. showLogo: {
  33. type: Boolean,
  34. default: false
  35. },
  36. // 是否需要渐变背景
  37. noBgColor: {
  38. type: Boolean,
  39. default: false
  40. },
  41. // 标题所在位置
  42. textLeft: {
  43. type: Boolean,
  44. default: true
  45. }
  46. })
  47. const defaultLogoHeight = 40
  48. const navbarHeight = ref(0)
  49. const statusBarHeight = ref(0)
  50. onLoad(() => {
  51. try {
  52. const systemInfo = uni.getSystemInfoSync()
  53. statusBarHeight.value = systemInfo.statusBarHeight
  54. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  55. // 计算导航栏高度
  56. navbarHeight.value = menuButtonInfo.height + 2 * (menuButtonInfo.top - statusBarHeight.value) + statusBarHeight.value
  57. // 确保导航栏高度不小于 logo 高度 + 10
  58. if (navbarHeight.value < defaultLogoHeight + 10) {
  59. navbarHeight.value = defaultLogoHeight + 10
  60. }
  61. // 将 navbarHeight 存入本地缓存
  62. const height = navbarHeight.value < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight.value + 10
  63. uni.setStorageSync('navbarHeight', height)
  64. } catch (error) {
  65. console.error('获取系统信息或菜单按钮信息时出错:', error)
  66. }
  67. })
  68. const goBack = () => {
  69. uni.navigateBack({
  70. delta: 1
  71. });
  72. }
  73. </script>
  74. <style scoped lang="scss">
  75. .gradientBgc {
  76. background: linear-gradient(180deg, #9bfece, #BCFEDE);
  77. }
  78. .navbar {
  79. position: fixed;
  80. top: 0;
  81. left: 0;
  82. width: 100%;
  83. z-index: 999;
  84. &-box {
  85. position: relative;
  86. width: 100%;
  87. height: 100%;
  88. &-logo {
  89. position: absolute;
  90. left: 10px;
  91. width: 95px;
  92. }
  93. }
  94. }
  95. .nav-box-title {
  96. position: relative;
  97. width: 100%;
  98. height: 100%;
  99. display: flex;
  100. align-items: center;
  101. .title-text {
  102. position: absolute;
  103. width: 158px;
  104. height: 51px;
  105. line-height: 51px;
  106. color: #0E100F;
  107. font-style: normal;
  108. text-transform: none;
  109. }
  110. }
  111. .text-left {
  112. left: 10px;
  113. font-family: MiSans-Semibold;
  114. font-weight: 600;
  115. font-size: 38rpx;
  116. }
  117. .text-center {
  118. left: 50%;
  119. transform: translateX(-50%);
  120. font-family: MiSans-Medium;
  121. font-size: 33rpx;
  122. }
  123. </style>