navbar.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // 获取状态栏高度
  22. geStatusBarHeight() {
  23. return uni.getSystemInfoSync()['statusBarHeight']
  24. },
  25. // 获取导航栏高度
  26. getNavBarHeight() {
  27. // #ifdef MP-WEIXIN
  28. let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  29. // 导航栏高度 = 胶囊高度 + 上间距 + 下间距 + 微调 (menuButtonInfo.top - uni.getSystemInfoSync()['statusBarHeight'] = 上间距)
  30. let navbarHeight = menuButtonInfo.height + (menuButtonInfo.top - uni.getSystemInfoSync()[
  31. 'statusBarHeight']) * 2 + 2
  32. // #endif
  33. // #ifdef APP-PLUS || H5
  34. let navbarHeight = 44
  35. // #endif
  36. return navbarHeight
  37. },
  38. navigateBack() {
  39. uni.navigateBack()
  40. }
  41. }
  42. }
  43. </script>
  44. <style lang="scss" scoped>
  45. .navbar {
  46. position: fixed;
  47. top: 0;
  48. left: 0;
  49. width: 100%;
  50. z-index: 1996;
  51. .status-bar {
  52. background: #fff;
  53. }
  54. .bar-content {
  55. position: relative;
  56. }
  57. }
  58. </style>