navigate-bar.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="uni-pages">
  3. <view class="navigation" :style="{'background-color': bgcolor}">
  4. <!-- 自适应不同的手机机型,导航顶部留白部分 -->
  5. <view class="nav--bar-top" :style="{'height':menuButton.top +'px'}"></view>
  6. <!-- 导航栏中间部分 -->
  7. <view class="nav-bar-inner">
  8. <!-- 导航左边部分,是否显示操作胶囊 -->
  9. <view class="image-box" v-if="control === 'back'"
  10. :style="{'height':menuButton.height+'px','width':menuButton.width+'px'}">
  11. <image class="image" src="/static/arrow-left.svg" @click="clickBack"></image>
  12. <view class="image"></view>
  13. </view>
  14. <view class="image-box" v-else-if="control === 'back|home'"
  15. :style="{'height':menuButton.height+'px','width':menuButton.width+'px'}">
  16. <image class="image" src="/static/arrow-left.svg" @click="clickBack"></image>
  17. <view class="line"></view>
  18. <image class="image" src="/static/home.svg" @click="clickHome"></image>
  19. </view>
  20. <view class="image-box" v-else-if="control === 'home'"
  21. :style="{'height':menuButton.height+'px','width':menuButton.width+'px'}">
  22. <image class="image" src="/static/home.svg" @click="clickHome"></image>
  23. </view>
  24. <view class="base-box" :style="{'height':menuButton.height+'px','width':menuButton.width+'px'}" v-else>
  25. </view>
  26. <!-- 中间文字部分,可设置字体颜色 -->
  27. <view class="title" :style="[titleStyle]">{{title}}</view>
  28. <!-- 右边部分,占据小程序系统胶囊位置 -->
  29. <view class="base-box" :style="{'height':menuButton.height+'px','width':menuButton.width+'px'}"></view>
  30. </view>
  31. <!-- 设置胶囊离内容距离 -->
  32. <view class="nav-bar-buttom" style="height: 8px;"></view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import props from "./props"
  38. export default {
  39. mixins: [props],
  40. data() {
  41. return {
  42. menuButton: {},
  43. prevLength: 0,
  44. }
  45. },
  46. computed: {
  47. titleStyle() {
  48. let style = {}
  49. style.color = this.color
  50. return style
  51. }
  52. },
  53. methods: {
  54. //获取跳转页面的路径,用于返回操作
  55. getPrevPage() {
  56. const page = getCurrentPages()
  57. // console.log("page", page);
  58. this.prevLength = page.length;
  59. if (page.length == 2) {
  60. uni.setStorageSync("prevPage", page[0].$page.fullPath)
  61. }
  62. },
  63. // 胶囊--返回
  64. clickBack() {
  65. if (this.prevLength > 2) {
  66. uni.navigateBack()
  67. } else {
  68. let firstPage = uni.getStorageSync("prevPage")
  69. let split = firstPage.split('/')
  70. if (split[1] == 'pages' && split[2] !== "login") {
  71. uni.switchTab({
  72. url: firstPage,
  73. success: function() {
  74. var page = getCurrentPages().pop();
  75. if (page == undefined || page == null) {
  76. return
  77. }
  78. page.onLoad();
  79. }
  80. });
  81. } else {
  82. uni.navigateBack()
  83. }
  84. }
  85. },
  86. // 胶囊--首页
  87. clickHome() {
  88. uni.switchTab({
  89. url: "/pages/home/home"
  90. });
  91. }
  92. },
  93. beforeMount() {
  94. // 在挂载时获取不同机型胶囊按钮对应的属性值(宽高,胶囊距离顶部的距离)
  95. this.menuButton = uni.getMenuButtonBoundingClientRect()
  96. // console.log("this.menuButton", this.menuButton);
  97. this.getPrevPage();
  98. },
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. .uni-pages {
  103. .navigation {
  104. .nav-bar-inner {
  105. height: 32px;
  106. display: flex;
  107. justify-content: space-between;
  108. align-items: center;
  109. padding: 0 8px;
  110. .image-box {
  111. display: flex;
  112. justify-content: space-around;
  113. align-items: center;
  114. .line {
  115. width: 1px;
  116. height: 26px;
  117. background-color: rgba(#fff, 0.3);
  118. }
  119. .image {
  120. width: 26px;
  121. height: 26px;
  122. }
  123. }
  124. .title {
  125. font-weight: 500;
  126. font-size: 36rpx;
  127. }
  128. }
  129. .nav-bar-buttom {
  130. height: 10rpx;
  131. }
  132. }
  133. }
  134. </style>