my-nav.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view>
  3. <view class="nav-box" :style="{'height':height+'px','background':bgColor}">
  4. <!-- 自定义导航栏 -->
  5. <view class="status_bar" :style="{'height':statusBarHeight+'px'}">
  6. <!-- uni-ui这里是状态栏 -->
  7. </view>
  8. <!-- 胶囊位置信息 -->
  9. <view class="nav-main flex align-center justify-center" :style="{height: navBarHeight+'px'}">
  10. <view class="nav-main-back" @click="back" v-if="backIcon">
  11. <uni-icons type="back" size="26" color="#fff"></uni-icons>
  12. </view>
  13. <text class="nav-main-title">{{title}}</text>
  14. </view>
  15. </view>
  16. <view :style="{'height':height+'px','background':bgColor}" style="background-color: #fff;">
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. bgColor: {
  24. type: String,
  25. default: "#FFF"
  26. },
  27. backIcon: {
  28. type: Boolean,
  29. default: true
  30. },
  31. title: {
  32. type: String,
  33. default: ""
  34. }
  35. },
  36. data() {
  37. return {
  38. //总高度
  39. height: 0,
  40. //胶囊位置信息
  41. menuButtonRect: {},
  42. //状态栏的高度
  43. statusBarHeight: 0,
  44. //导航栏的高度
  45. navBarHeight: 0
  46. }
  47. },
  48. created() {
  49. // this.height = wx.getStorageSync('navBarHeight')
  50. this.getHeight();
  51. },
  52. methods: {
  53. //获取屏幕导航栏高度
  54. getHeight() {
  55. if (wx.canIUse('getMenuButtonBoundingClientRect')) {
  56. let sysInfo = wx.getSystemInfoSync(); //状态栏的高度
  57. this.statusBarHeight = sysInfo.statusBarHeight;
  58. // 胶囊位置信息
  59. let rect = wx.getMenuButtonBoundingClientRect();
  60. this.menuButtonRect = JSON.parse(JSON.stringify(rect));
  61. // 导航栏高度
  62. let navBarHeight = (rect.top - sysInfo.statusBarHeight) * 2 + rect.height;
  63. this.navBarHeight = navBarHeight;
  64. // 自定义导航栏的高度
  65. this.height = sysInfo.statusBarHeight + navBarHeight;
  66. } else {
  67. wx.showToast({
  68. title: '您的微信版本过低,界面可能会显示不正常',
  69. icon: 'none',
  70. duration: 4000
  71. });
  72. }
  73. },
  74. //返回
  75. back() {
  76. uni.navigateBack({
  77. delta: 1
  78. })
  79. },
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .nav-box{
  85. width: 100%;
  86. background-color: #fff;
  87. position: fixed;
  88. top: 0;
  89. z-index: 9999;
  90. overflow: hidden;
  91. }
  92. .status_bar {
  93. // height: var(--status-bar-height);
  94. width: 100%;
  95. // background:#ff0;
  96. }
  97. .nav-main {
  98. position: relative;
  99. display: flex;
  100. align-items: center;
  101. justify-content: center;
  102. // background:#f00;
  103. .nav-main-back {
  104. position: absolute;
  105. left: 10rpx;
  106. }
  107. .nav-main-title {
  108. color: #333;
  109. font-size: 16px;
  110. }
  111. }
  112. </style>