index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. const systemInfo = uni.getSystemInfoSync()
  49. statusBarHeight.value = systemInfo.statusBarHeight
  50. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  51. navbarHeight.value = menuButtonInfo.height + ((menuButtonInfo.top - statusBarHeight.value) * 2) + 15
  52. // 将navbarHeight存入本地缓存
  53. const height = navbarHeight.value < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight.value + 10
  54. uni.setStorageSync('navbarHeight', height || 65)
  55. })
  56. defineExpose({
  57. height: navbarHeight.value < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight.value + 10
  58. })
  59. </script>
  60. <style scoped lang="scss">
  61. .gradientBgc {
  62. background: linear-gradient(180deg, #9bfece, #BCFEDE);
  63. }
  64. .navbar {
  65. position: fixed;
  66. top: 0;
  67. left: 0;
  68. width: 100%;
  69. z-index: 999;
  70. &-box {
  71. position: relative;
  72. width: 100%;
  73. height: 100%;
  74. &-logo {
  75. position: absolute;
  76. left: 10px;
  77. width: 95px;
  78. }
  79. &-title {
  80. position: absolute;
  81. width: 158px;
  82. height: 51px;
  83. line-height: 51px;
  84. color: #0E100F;
  85. font-style: normal;
  86. text-transform: none;
  87. }
  88. }
  89. }
  90. .text-left {
  91. left: 10px;
  92. font-family: MiSans-Semibold;
  93. font-weight: 600;
  94. font-size: 38rpx;
  95. }
  96. .text-center {
  97. left: 50%;
  98. transform: translateX(-50%);
  99. font-family: MiSans-Medium;
  100. font-size: 38rpx;
  101. }
  102. </style>