navbar.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view>
  3. <view class="head" :style="{ height: height + 'px', background:background}">
  4. <!-- <view
  5. class="back"
  6. @click="back"
  7. v-if="backShow"
  8. :style="{
  9. top: top + 'px',
  10. color:color,
  11. fontSize:'24rpx'
  12. }">〈 {{backtext}}</view> -->
  13. <view
  14. class="back"
  15. @click="back"
  16. v-if="backShow"
  17. :style="{
  18. top: top + 'px',
  19. color:color,
  20. fontSize:'30rpx',
  21. textAlign:'center',
  22. }">〈 </view>
  23. <view
  24. class="title"
  25. v-if="titleShow"
  26. :style="{
  27. top: top + 'px',
  28. color:color,
  29. 'font-size': size + 'rpx'
  30. }">{{title}}</view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import { getCurrentInstance } from 'vue';
  36. export default {
  37. name:"navbar",
  38. props: {
  39. backtext: {
  40. type: String,
  41. default: '返回'
  42. },
  43. title: {
  44. type: String,
  45. default: '自定义标题'
  46. },
  47. size:{
  48. type: Number,
  49. default: 32
  50. },
  51. color:{
  52. type: String,
  53. default: '#000'
  54. },
  55. titleShow: {
  56. type: Boolean,
  57. default: true
  58. },
  59. backShow: {
  60. type: Boolean,
  61. default: true
  62. },
  63. background:{
  64. type: String,
  65. // default: "linear-gradient(to right, #43e97b 0%, #38f9d7 100%)"
  66. default:"#3f73b3"
  67. }
  68. },
  69. data() {
  70. return {
  71. height :32,
  72. top:0,
  73. }
  74. },
  75. created() {
  76. //设备信息
  77. let app = uni.getSystemInfoSync();
  78. console.log('设备信息',app)
  79. // #ifdef APP-PLUS
  80. this.top = app.safeArea.top
  81. this.height = 64 + app.safeAreaInsets.bottom
  82. //#endif
  83. // #ifdef MP-WEIXIN
  84. //胶囊信息
  85. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  86. this.height = app.statusBarHeight + menuButtonInfo.height + (menuButtonInfo.top - app.statusBarHeight)*2
  87. this.top = menuButtonInfo.top;
  88. //#endif
  89. },
  90. methods: {
  91. back(){
  92. console.log('点击头部返回')
  93. let page = getCurrentPages();
  94. let beforPage = page[page.length-2]
  95. uni.navigateBack({
  96. success() {
  97. beforPage.onShow();
  98. }
  99. });
  100. }
  101. }
  102. }
  103. </script>
  104. <style>
  105. .head{
  106. position: fixed;
  107. width: 100vw;
  108. z-index: 999;
  109. padding: 0.5em;
  110. top:0;
  111. left:0;
  112. }
  113. .title{
  114. position: fixed;
  115. /* width: 750rpx; */
  116. width: 100%;
  117. text-align: center;
  118. line-height: 32px;
  119. height: 32px;
  120. }
  121. .back{
  122. position: fixed;
  123. /* width: 81rpx; */
  124. text-align: center;
  125. line-height: 32px;
  126. height: 32px;
  127. font-size: 32rpx;
  128. /* font-weight: 200; */
  129. z-index: 9;
  130. }
  131. </style>