index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view
  3. class="navbar"
  4. :class="{'gradientBgc': !noBgColor}"
  5. :style="{
  6. 'height': height + 'px',
  7. 'line-height': height + '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" :style="{'height': height ? (height - statusBarHeight) + 'px' : '100%'}">
  18. <uni-icons v-if="!textLeft" 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. const height = ref(0)
  51. onLoad(async () => {
  52. try {
  53. const systemInfo = uni.getSystemInfoSync()
  54. statusBarHeight.value = systemInfo.statusBarHeight
  55. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  56. // 计算导航栏高度
  57. navbarHeight.value = menuButtonInfo.height + 2 * (menuButtonInfo.top - statusBarHeight.value) + statusBarHeight.value
  58. // 确保导航栏高度不小于 logo 高度 + 10
  59. if (navbarHeight.value < defaultLogoHeight + 10) {
  60. navbarHeight.value = defaultLogoHeight + 10
  61. }
  62. height.value = navbarHeight.value < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight.value + 10
  63. await new Promise((resolve, reject) => {
  64. uni.setStorage({
  65. key: 'navbarHeight',
  66. data: height.value,
  67. success: resolve,
  68. fail: reject
  69. })
  70. })
  71. } catch (error) {
  72. console.error('获取系统信息或菜单按钮信息时出错:', error)
  73. }
  74. })
  75. // 返回上一页
  76. const goBack = () => {
  77. uni.navigateBack({
  78. delta: 1
  79. })
  80. }
  81. </script>
  82. <style scoped lang="scss">
  83. .gradientBgc {
  84. background: linear-gradient(180deg, #9bfece, #BCFEDE);
  85. }
  86. .navbar {
  87. position: fixed;
  88. top: 0;
  89. left: 0;
  90. width: 100%;
  91. z-index: 999;
  92. &-box {
  93. position: relative;
  94. width: 100%;
  95. height: 100%;
  96. &-logo {
  97. position: absolute;
  98. left: 10px;
  99. width: 95px;
  100. }
  101. }
  102. }
  103. .nav-box-title {
  104. position: relative;
  105. width: 100%;
  106. display: flex;
  107. align-items: center;
  108. .title-text {
  109. position: absolute;
  110. width: 158px;
  111. height: 51px;
  112. line-height: 51px;
  113. color: #0E100F;
  114. font-style: normal;
  115. text-transform: none;
  116. }
  117. }
  118. .text-left {
  119. left: 10px;
  120. font-family: MiSans-Semibold;
  121. font-weight: 600;
  122. font-size: 38rpx;
  123. }
  124. .text-center {
  125. left: 50%;
  126. transform: translateX(-50%);
  127. font-family: MiSans-Medium;
  128. font-size: 33rpx;
  129. }
  130. </style>