nav-bar.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <view 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. titleBarHeight: 0,
  37. statusBarHeight: 0,
  38. isNotchScreen: false
  39. }
  40. },
  41. created() {
  42. const windowInfo = uni.getWindowInfo()
  43. this.statusBarHeight = windowInfo.statusBarHeight
  44. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  45. this.titleBarHeight = (menuButtonInfo.top - this.statusBarHeight) * 2 + menuButtonInfo.height + (windowInfo.screenWidth / 750 * 16)
  46. this.$emit('init', {
  47. detail: {
  48. navbarHeight: this.statusBarHeight + this.titleBarHeight,
  49. pageHeight: windowInfo.screenHeight - (this.statusBarHeight + this.titleBarHeight)
  50. }
  51. })
  52. },
  53. methods: {
  54. hasSlot() {
  55. const nodes = this.getRelationNodes(`view`);
  56. return nodes.length > 0;
  57. },
  58. goBack() {
  59. uni.navigateBack()
  60. }
  61. }
  62. }
  63. </script>
  64. <style lang="scss">
  65. .nav-bar {
  66. background: linear-gradient( 270deg, #2b2359 9%, #405491 44%, #2b2359 92%), rgba(0,0,0,0.2);
  67. &.nav-bar-transparent{
  68. background: none;
  69. .nav-bar-title{
  70. view{
  71. color: #333333;
  72. }
  73. }
  74. }
  75. .nav-bar-title{
  76. display: flex;
  77. justify-content: space-between;
  78. align-items: center;
  79. padding: 0 32rpx;
  80. view{
  81. color: #ffffff;
  82. }
  83. }
  84. .nav-bar-title-text{
  85. flex: 1;
  86. font-weight: 500;
  87. font-size: $fontSize6;
  88. line-height: 42rpx;
  89. text-align: left;
  90. text-transform: none;
  91. padding-left: 8rpx;
  92. }
  93. .btn-back{
  94. padding: 0 6rpx;
  95. }
  96. }
  97. </style>