nav-bar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view v-if="!hideNavbar" class="nav-bar" :class="{ 'nav-bar-transparent': transparent,'hide_nav_bar':hideNavbar }">
  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. self.$emit('init', {
  55. detail: {
  56. navbarHeight: 0,
  57. pageHeight: windowInfo.screenHeight
  58. }
  59. })
  60. self.hideNavbar = true
  61. // self.statusBarHeight = 0
  62. // self.titleBarHeight = (windowInfo.screenWidth / 750 * 100)
  63. // self.$emit('init', {
  64. // detail: {
  65. // navbarHeight: (windowInfo.screenWidth / 750 * 100),
  66. // pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 100)
  67. // }
  68. // })
  69. } else {
  70. self.$emit('init', {
  71. detail: {
  72. navbarHeight: self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight),
  73. pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight)) - res.safeAreaInsets.bottom
  74. }
  75. })
  76. }
  77. }
  78. })
  79. } else {
  80. uni.getSystemInfo({
  81. success(res) {
  82. const os = res.system.toLowerCase()
  83. self.$config.os = os
  84. if (os.includes('windows') || os.includes('mac')) {
  85. self.statusBarHeight = 0
  86. self.titleBarHeight = (windowInfo.screenWidth / 750 * 120)
  87. self.$emit('init', {
  88. detail: {
  89. navbarHeight: (windowInfo.screenWidth / 750 * 120),
  90. pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 120)
  91. }
  92. })
  93. } else {
  94. self.$emit('init', {
  95. detail: {
  96. navbarHeight: self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight),
  97. pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight))- res.safeAreaInsets.bottom
  98. }
  99. })
  100. }
  101. }
  102. })
  103. }
  104. // this.$emit('init', {
  105. // detail: {
  106. // navbarHeight: self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight),
  107. // pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight))
  108. // }
  109. // })
  110. },
  111. methods: {
  112. hasSlot() {
  113. const nodes = this.getRelationNodes(`view`);
  114. return nodes.length > 0;
  115. },
  116. goBack() {
  117. uni.navigateBack()
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .nav-bar {
  124. background: linear-gradient( 270deg, #2b2359 9%, #405491 44%, #2b2359 92%), rgba(0,0,0,0.2);
  125. width: 100%;
  126. position: fixed;
  127. left: 0;
  128. z-index: 10;
  129. top: 0;
  130. &.nav-bar-transparent{
  131. background: none;
  132. .nav-bar-title{
  133. view{
  134. color: #333333;
  135. }
  136. }
  137. }
  138. .nav-bar-title{
  139. display: flex;
  140. justify-content: space-between;
  141. align-items: center;
  142. padding: 0 32rpx;
  143. view{
  144. color: #ffffff;
  145. }
  146. }
  147. .nav-bar-title-text{
  148. flex: 1;
  149. font-weight: 500;
  150. font-size: $fontSize6;
  151. line-height: 42rpx;
  152. text-align: left;
  153. text-transform: none;
  154. padding-left: 8rpx;
  155. }
  156. .btn-back{
  157. padding: 0 6rpx;
  158. cursor: pointer;
  159. }
  160. }
  161. </style>