nav-bar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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' }"></view>
  4. <view class="nav-bar-title">
  5. <template v-if="showTitle">
  6. <view class="btn-back" @click="goBack">
  7. <van-icon name="arrow-left" />
  8. </view>
  9. <view class="nav-bar-title-text">{{ title }} </view>
  10. <view></view>
  11. </template>
  12. <template v-else>
  13. <slot></slot>
  14. </template>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. props: {
  21. title: String,
  22. transparent: Boolean,
  23. showTitle: {
  24. type: Boolean,
  25. default: true
  26. }
  27. },
  28. computed: {
  29. hasParent() {
  30. return getCurrentPages().length > 1
  31. }
  32. },
  33. data() {
  34. return {
  35. hideNavbar: false,
  36. titleBarHeight: 0,
  37. statusBarHeight: 0,
  38. isNotchScreen: false
  39. }
  40. },
  41. created() {
  42. const self = this
  43. const windowInfo = uni.getWindowInfo()
  44. this.statusBarHeight = windowInfo.statusBarHeight
  45. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  46. this.titleBarHeight = (menuButtonInfo.top - this.statusBarHeight) * 2 + menuButtonInfo.height + (windowInfo.screenWidth / 750 * 16)
  47. if (this.showTitle) {
  48. uni.getSystemInfo({
  49. success(res) {
  50. const os = res.system.toLowerCase()
  51. self.$config.os = os
  52. if (os.includes('windows') || os.includes('mac')) {
  53. self.$emit('init', {
  54. detail: {
  55. navbarHeight: 0,
  56. pageHeight: windowInfo.screenHeight
  57. }
  58. })
  59. self.hideNavbar = true
  60. // self.statusBarHeight = 0
  61. // self.titleBarHeight = (windowInfo.screenWidth / 750 * 100)
  62. // self.$emit('init', {
  63. // detail: {
  64. // navbarHeight: (windowInfo.screenWidth / 750 * 100),
  65. // pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 100)
  66. // }
  67. // })
  68. } else {
  69. self.$emit('init', {
  70. detail: {
  71. navbarHeight: self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight),
  72. pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight)) - res.safeAreaInsets.bottom
  73. }
  74. })
  75. }
  76. }
  77. })
  78. } else {
  79. uni.getSystemInfo({
  80. success(res) {
  81. const os = res.system.toLowerCase()
  82. self.$config.os = os
  83. if (os.includes('windows') || os.includes('mac')) {
  84. self.statusBarHeight = 0
  85. self.titleBarHeight = (windowInfo.screenWidth / 750 * 120)
  86. self.$emit('init', {
  87. detail: {
  88. navbarHeight: (windowInfo.screenWidth / 750 * 120),
  89. pageHeight: windowInfo.screenHeight - (windowInfo.screenWidth / 750 * 120)
  90. }
  91. })
  92. } else {
  93. self.$emit('init', {
  94. detail: {
  95. navbarHeight: self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight),
  96. pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (self.statusBarHeight + self.titleBarHeight))- res.safeAreaInsets.bottom
  97. }
  98. })
  99. }
  100. }
  101. })
  102. }
  103. // this.$emit('init', {
  104. // detail: {
  105. // navbarHeight: self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight),
  106. // pageHeight: windowInfo.screenHeight - (self.hideNavbar ? 0 : (this.statusBarHeight + this.titleBarHeight))
  107. // }
  108. // })
  109. },
  110. methods: {
  111. hasSlot() {
  112. const nodes = this.getRelationNodes(`view`);
  113. return nodes.length > 0;
  114. },
  115. goBack() {
  116. this.refreshData()
  117. if (this.hasParent) {
  118. uni.navigateBack()
  119. } else if(this.$store.getters.page.length > 0) {
  120. this.$store.commit('DELETE_PAGE')
  121. console.log(this.$store.getters.page)
  122. } else {
  123. this.navigateTo('/pages/index/index')
  124. }
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .nav-bar {
  131. background: linear-gradient( 270deg, #2b2359 9%, #405491 44%, #2b2359 92%), rgba(0,0,0,0.2);
  132. width: 100%;
  133. position: fixed;
  134. left: 0;
  135. z-index: 10;
  136. height: $navBarHeight;
  137. top: 0;
  138. display: flex;
  139. flex-direction: column;
  140. &.nav-bar-transparent{
  141. background: none;
  142. .nav-bar-title{
  143. view{
  144. color: #333333;
  145. }
  146. }
  147. }
  148. .nav-bar-title{
  149. display: flex;
  150. justify-content: space-between;
  151. align-items: center;
  152. padding: 0 16rpx;
  153. flex: 1;
  154. min-height: 1px;
  155. view{
  156. color: #ffffff;
  157. }
  158. }
  159. .nav-bar-title-text{
  160. flex: 1;
  161. font-weight: 500;
  162. font-size: $fontSize5;
  163. //line-height: 42rpx;
  164. line-height: 1.5;
  165. text-align: left;
  166. text-transform: none;
  167. padding-left: 8rpx;
  168. }
  169. .btn-back{
  170. padding: 0 6rpx;
  171. cursor: pointer;
  172. }
  173. }
  174. </style>