nav-bar.vue 4.1 KB

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