custom-nav.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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="showBack?back():''">
  5. <text v-if="showBack" 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. },
  27. computed: {
  28. style() {
  29. let _style = `height: ${this.customBarH}px;margin-bottom:${this.statusBarH}px`
  30. return _style
  31. }
  32. },
  33. methods: {
  34. back() {
  35. console.log(111)
  36. if (this.toBack) {
  37. this.$parent.toBack()
  38. return false
  39. }
  40. uni.navigateBack()
  41. }
  42. },
  43. mounted() {
  44. let menuButtonObject = uni.getMenuButtonBoundingClientRect(); //获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。
  45. uni.getSystemInfo({ //获取系统信息
  46. success: res => {
  47. let navHeight = menuButtonObject.height + (menuButtonObject.top - res.statusBarHeight) *
  48. 2; //导航栏高度=菜单按钮高度+(菜单按钮与顶部距离-状态栏高度)*2
  49. // 导航栏(胶囊)高度
  50. this.customBarH = navHeight;
  51. // 状态栏(顶部)高度
  52. this.statusBarH = res.statusBarHeight
  53. },
  54. fail(err) {
  55. console.log(err);
  56. }
  57. })
  58. },
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .header {
  63. width: 100vw;
  64. position: relative;
  65. z-index: 10;
  66. margin: 0;
  67. }
  68. .flexbox {
  69. width: 100%;
  70. padding: 0 20px;
  71. font-size: 40rpx;
  72. .back {
  73. margin-right: 8rpx;
  74. }
  75. }
  76. </style>