custom-nav.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view class="header" :style="style">
  3. <view class="flexbox"
  4. :style="[{'height': customBarH + 'px', 'line-height': customBarH + 'px','padding-top': statusBarH + 'px', 'color': statusBarColor, 'background': bgColor}]" @tap="showIndex?toIndex():showBack?back():''">
  5. <text v-if="showBack||showIndex" class="back cuIcon-back"></text>
  6. <text class="title">{{ title }}</text>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. statusBarH: 0,
  15. customBarH: 0
  16. }
  17. },
  18. props: {
  19. title: String,
  20. statusBarColor: String,
  21. bgColor: String,
  22. showBack: {
  23. type: Boolean,
  24. default: true
  25. },
  26. showIndex:{
  27. type: Boolean,
  28. default: false
  29. }
  30. },
  31. computed: {
  32. style() {
  33. let _style = `height: ${this.customBarH}px;margin-bottom:${this.statusBarH}px`
  34. return _style
  35. }
  36. },
  37. methods: {
  38. back() {
  39. console.log(111)
  40. if (this.toBack) {
  41. this.$parent.toBack()
  42. return false
  43. }
  44. uni.navigateBack()
  45. },
  46. //处理【其他】页面返回按钮问题
  47. toIndex(){
  48. uni.redirectTo({
  49. url:'/pages/index/index'
  50. })
  51. }
  52. },
  53. mounted() {
  54. let menuButtonObject = uni.getMenuButtonBoundingClientRect(); //获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。
  55. uni.getSystemInfo({ //获取系统信息
  56. success: res => {
  57. let navHeight = menuButtonObject.height + (menuButtonObject.top - res.statusBarHeight) *
  58. 2; //导航栏高度=菜单按钮高度+(菜单按钮与顶部距离-状态栏高度)*2
  59. // 导航栏(胶囊)高度
  60. this.customBarH = navHeight;
  61. // 状态栏(顶部)高度
  62. this.statusBarH = res.statusBarHeight
  63. },
  64. fail(err) {
  65. console.log(err);
  66. }
  67. })
  68. },
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. .header {
  73. width: 100vw;
  74. position: relative;
  75. z-index: 10;
  76. margin: 0;
  77. }
  78. .flexbox {
  79. width: 100%;
  80. padding: 0 20px;
  81. font-size: 40rpx;
  82. .back {
  83. margin-right: 8rpx;
  84. }
  85. }
  86. </style>