nav-bar.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view v-if="!hideNavbar" class="nav-bar" :class="{ 'nav-bar-transparent': transparent }">
  3. <view class="nav-bar-status" :style="{ height: statusBarHeight + 'px' }">
  4. </view>
  5. <view class="nav-bar-title" :style="{ height: titleBarHeight +'px' }">
  6. <template v-if="showTitle">
  7. <view class="btn-back" @click="goBack">
  8. <van-icon v-if="hasParent" name="arrow-left" />
  9. </view>
  10. <view class="nav-bar-title-text">{{ title }} </view>
  11. <view></view>
  12. </template>
  13. <template v-else>
  14. <slot></slot>
  15. </template>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. title: String,
  23. transparent: Boolean,
  24. showTitle: {
  25. type: Boolean,
  26. default: true
  27. }
  28. },
  29. computed: {
  30. hasParent() {
  31. return getCurrentPages().length > 1
  32. }
  33. },
  34. data() {
  35. return {
  36. hideNavbar: false,
  37. titleBarHeight: 0,
  38. statusBarHeight: 0,
  39. isNotchScreen: false
  40. }
  41. },
  42. created() {
  43. const self = this
  44. const windowInfo = uni.getWindowInfo()
  45. this.statusBarHeight = windowInfo.statusBarHeight
  46. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  47. this.titleBarHeight = (menuButtonInfo.top - this.statusBarHeight) * 2 + menuButtonInfo.height + (windowInfo.screenWidth / 750 * 16)
  48. if (this.showTitle) {
  49. uni.getSystemInfo({
  50. success(res) {
  51. const os = res.system.toLowerCase()
  52. self.$config.os = os
  53. if (os.includes('windows') || os.includes('mac')) {
  54. // 可在pc端隐藏导航
  55. self.$emit('init', {
  56. detail: {
  57. navbarHeight: 0,
  58. pageHeight: windowInfo.screenHeight
  59. }
  60. })
  61. self.hideNavbar = true
  62. // self.statusBarHeight = 0
  63. // self.titleBarHeight = (windowInfo.screenWidth / 750 * 100)
  64. // self.$emit('init', {
  65. // detail: {
  66. // navbarHeight: (windowInfo.screenWidth / 750 * 100),
  67. // pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 100)
  68. // }
  69. // })
  70. }
  71. }
  72. })
  73. } else {
  74. uni.getSystemInfo({
  75. success(res) {
  76. const os = res.system.toLowerCase()
  77. self.$config.os = os
  78. if (os.includes('windows') || os.includes('mac')) {
  79. self.statusBarHeight = 0
  80. self.titleBarHeight = (windowInfo.screenWidth / 750 * 120)
  81. self.$emit('init', {
  82. detail: {
  83. navbarHeight: (windowInfo.screenWidth / 750 * 120),
  84. pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 120)
  85. }
  86. })
  87. }
  88. }
  89. })
  90. }
  91. this.$emit('init', {
  92. detail: {
  93. navbarHeight: self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight),
  94. pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight))
  95. }
  96. })
  97. },
  98. methods: {
  99. hasSlot() {
  100. const nodes = this.getRelationNodes(`view`);
  101. return nodes.length > 0;
  102. },
  103. goBack() {
  104. uni.navigateBack()
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss">
  110. .nav-bar {
  111. background: linear-gradient( 270deg, #2b2359 9%, #405491 44%, #2b2359 92%), rgba(0,0,0,0.2);
  112. &.nav-bar-transparent{
  113. background: none;
  114. .nav-bar-title{
  115. view{
  116. color: #333333;
  117. }
  118. }
  119. }
  120. .nav-bar-title{
  121. display: flex;
  122. justify-content: space-between;
  123. align-items: center;
  124. padding: 0 32rpx;
  125. view{
  126. color: #ffffff;
  127. }
  128. }
  129. .nav-bar-title-text{
  130. flex: 1;
  131. font-weight: 500;
  132. font-size: $fontSize6;
  133. line-height: 42rpx;
  134. text-align: left;
  135. text-transform: none;
  136. padding-left: 8rpx;
  137. }
  138. .btn-back{
  139. padding: 0 6rpx;
  140. cursor: pointer;
  141. }
  142. }
  143. </style>