navbar.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <view class="navbar">
  3. <!-- 状态栏高度 -->
  4. <view class="status-bar" :style="{'height': geStatusBarHeight() + 'px'}"></view>
  5. <!-- 导航栏高度 -->
  6. <view class="bar-content" :style="{'height': getNavBarHeight()+'px'}">
  7. <slot></slot>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'Navbar',
  14. props: {
  15. title: {
  16. type: String,
  17. default: ''
  18. }
  19. },
  20. methods: {
  21. geStatusBarHeight() {
  22. return uni.getSystemInfoSync()['statusBarHeight']
  23. },
  24. getNavBarHeight() {
  25. // #ifdef MP-WEIXIN
  26. let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  27. // 导航栏高度 = 胶囊高度 + 上间距 + 下间距 + 微调 (menuButtonInfo.top - uni.getSystemInfoSync()['statusBarHeight'] = 上间距)
  28. let navbarHeight = menuButtonInfo.height + (menuButtonInfo.top - uni.getSystemInfoSync()[
  29. 'statusBarHeight']) * 2 + 4
  30. // #endif
  31. // #ifdef APP-PLUS || H5
  32. let navbarHeight = 44
  33. // #endif
  34. return navbarHeight
  35. },
  36. navigateBack() {
  37. uni.navigateBack()
  38. }
  39. }
  40. }
  41. </script>
  42. <style lang="scss" scoped>
  43. .navbar {
  44. position: fixed;
  45. top: 0;
  46. left: 0;
  47. width: 100%;
  48. z-index: 1996;
  49. .status-bar {
  50. background: #fff;
  51. }
  52. .bar-content {
  53. position: relative;
  54. }
  55. }
  56. </style>