index.vue 2.8 KB

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