navbar.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view class="ua__navbar">
  3. <view class="ua__navbar-wrap" :class="{'custom': custom, 'fixed': fixed || transparent}"
  4. :style="{'height': customBarH + 'px', 'padding-top': (custom ? statusBarH : 0) + 'px', 'background': bgcolor, 'color': color, 'z-index': zIndex}">
  5. <!-- //左侧 (返回) -->
  6. <view class="action navbar-action__left" v-if="back && back!='false'" @click="onBack">
  7. <template v-if="$slots.back">
  8. <slot name="back" />
  9. </template>
  10. <template v-else>
  11. <view class="navbar-back"><image src="@/static/images/icon_zt_back.png"></image></view>
  12. </template>
  13. <slot name="backText" />
  14. </view>
  15. <slot name="left" />
  16. <!-- //标题 -->
  17. <view v-if="!search" class="navbar-title" :class="{'center': center}">
  18. <template v-if="$slots.title">
  19. <slot name="title" />
  20. </template>
  21. <template v-else><text :style="{'color': color}">{{title}}</text></template>
  22. </view>
  23. <!-- //搜索框 -->
  24. <view v-if="search" class="action navbar-action__search">
  25. <slot name="search" />
  26. </view>
  27. <!-- //右侧 -->
  28. <view class="action navbar-action__right">
  29. <slot name="right" />
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. props: {
  37. // 是否采用自定义导航模式
  38. custom: {
  39. type: [Boolean, String],
  40. default: false
  41. },
  42. // 是否返回
  43. back: {
  44. type: [Boolean, String],
  45. default: true
  46. },
  47. // 标题
  48. title: {
  49. type: String,
  50. default: ''
  51. },
  52. // 标题颜色
  53. color: {
  54. type: String,
  55. default: '#353535'
  56. },
  57. // 背景色
  58. bgcolor: {
  59. type: String,
  60. default: '#fff'
  61. },
  62. // 标题是否居中
  63. center: {
  64. type: [Boolean, String],
  65. default: false
  66. },
  67. // 搜索框
  68. search: {
  69. type: [Boolean, String],
  70. default: false
  71. },
  72. // 是否固定导航
  73. fixed: {
  74. type: [Boolean, String],
  75. default: false
  76. },
  77. // 是否背景透明
  78. transparent: {
  79. type: [Boolean, String],
  80. default: false
  81. },
  82. // 设置层叠
  83. zIndex: {
  84. type: [Number, String],
  85. default: '2022'
  86. },
  87. },
  88. data() {
  89. return {
  90. // 全局设置状态栏和导航栏高度
  91. statusBarH: 0,
  92. customBarH: 0,
  93. }
  94. },
  95. mounted() {
  96. // console.log(this.bgcolor)
  97. var that = this;
  98. // console.log(that.$http._GET.customBarH)
  99. // console.log(that.$http._GET.statusBarH)
  100. if(that.$http._GET.customBarH!=0){
  101. that.statusBarH = that.$http._GET.statusBarH
  102. // 状态栏
  103. that.customBarH = that.$http._GET.customBarH
  104. }else{
  105. uni.getSystemInfo({
  106. success(e) {
  107. // console.log(e)
  108. // 获取手机状态栏高度
  109. let statusBar = e.statusBarHeight
  110. let customBar
  111. // #ifndef MP
  112. customBar = statusBar + (e.platform == 'android' ? 50 : 45)
  113. // #endif
  114. // #ifdef MP-WEIXIN
  115. // 获取胶囊按钮的布局位置信息
  116. let menu = wx.getMenuButtonBoundingClientRect()
  117. // 导航栏高度 = 胶囊下距离 + 胶囊上距离 - 状态栏高度
  118. customBar = menu.bottom + menu.top - statusBar
  119. // #endif
  120. // #ifdef MP-ALIPAY
  121. customBar = statusBar + e.titleBarHeight
  122. // #endif
  123. // console.log(statusBar,customBar)
  124. // 导航栏
  125. that.statusBarH = statusBar
  126. // 状态栏
  127. that.customBarH = that.custom ? customBar : customBar - statusBar
  128. that.$http._GET.customBarH=that.customBarH;
  129. that.$http._GET.statusBarH=that.statusBarH;
  130. that.$emit('getTop',that.customBarH)
  131. },
  132. })
  133. }
  134. },
  135. methods: {
  136. onBack() {
  137. uni.navigateBack({
  138. delta: 1
  139. })
  140. }
  141. }
  142. }
  143. </script>
  144. <style scoped lang="scss">
  145. .ua__navbar-wrap{box-sizing: border-box;display: flex;align-items: center;}
  146. .ua__navbar-wrap.fixed{position: fixed;left: 0;top: 0;right: 0;}
  147. .navbar-title.center {
  148. text-align: center;
  149. flex: 1;
  150. }
  151. .navbar-back{flex: 0 0 auto;display: flex;align-items: center;justify-content: center;
  152. // padding: 0 20rpx;
  153. width: 88rpx;height: 88rpx;
  154. image{width: 18rpx;height: 34rpx;}
  155. }
  156. </style>