headerNavbar.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template name="headerNavbar">
  2. <!-- 自定义导航栏 -->
  3. <view class='navbar-wrap' :style="{height:navbarHeight+'px',paddingTop:statusBarHeight+'px'}"> 
  4.   <view class="navbar-text" :style="{lineHeight:(navbarHeight - statusBarHeight)+'px;',fontSize:fontSizeSetting+'px;'}">
  5.     {{navbarData.title ? navbarData.title : " "}}
  6.   </view>
  7.   <view class="navbar-icon" v-if="navbarData.showCapsule ? navbarData.showCapsule : true"
  8. :style="{width:headerBtnPosi.width+'px;',lineHeight:(navbarBtn.height)+'px',top:navbarBtn.top + statusBarHeight+'px;',left:navbarBtn.right+'px;',height:(navbarBtn.height)+'px;'}">
  9. <text v-if='haveBack' @click="_goBack" class="iconfont icon-shangyibu" :style="{width:headerBtnPosi.width/2+'px',height:navbarBtn.height+'px'}"></text>
  10.       <text v-if='haveBack' class="iconfont icon-vertical_line" :style="{borderColor:navbarData.borderColor ? navbarData.borderColor : 'rgba(0,0,0,0.4)'}"></text>
  11. <text @click="_goHome" class="iconfont icon-shouye" :style="{width:headerBtnPosi.width/2+'px',height:navbarBtn.height+'px'}"></text>
  12.   </view>
  13. </view>
  14. </template>
  15. <script>
  16. var self;
  17. export default{
  18. name:'headerNavbar',
  19. props:{
  20. navbarData: { // 由父页面传递的数据
  21. type: Object
  22. },
  23. systeminfo:{
  24. type:Object
  25. },
  26. headerBtnPosi:{
  27. type:Object
  28. },
  29. page:{
  30. type:Number
  31. }
  32. },
  33. data() {
  34. return{
  35. haveBack: true, // 是否有返回按钮,true 有 false 没有 若从分享页进入则为 false
  36.     statusBarHeight: 0, // 状态栏高度
  37.     navbarHeight: 0, // 顶部导航栏高度
  38.     navbarBtn: { // 胶囊位置信息
  39.       height: 0,
  40.       width: 0,
  41.       top: 0,
  42.       bottom: 0,
  43.       right: 0
  44. },
  45. platform:'',
  46. fontSizeSetting:0,
  47. screenWidth:0
  48. }
  49. },
  50. created() {
  51. // console.log(this.headerBtnPosi);
  52. this.fontSizeSetting = this.systeminfo.fontSizeSetting
  53. let statusBarHeight = this.systeminfo.statusBarHeight // 状态栏高度
  54. let headerPosi = this.headerBtnPosi // 胶囊位置信息
  55. console.log(this.fontSizeSetting)
  56.     /**
  57.      * wx.getMenuButtonBoundingClientRect() 坐标信息以屏幕左上角为原点
  58.      * 菜单按键宽度: 87
  59.      * 菜单按键高度: 32
  60.      * 菜单按键左边界坐标: 278
  61.      * 菜单按键上边界坐标: 26
  62.      * 菜单按键右边界坐标: 365
  63.      * 菜单按键下边界坐标: 58
  64.      */
  65. let btnPosi = { // 胶囊实际位置,坐标信息不是左上角原点
  66.       height: headerPosi.height,
  67.       width: headerPosi.width,
  68.       // 胶囊top - 状态栏高度
  69.       top: headerPosi.top - statusBarHeight,
  70.       // 胶囊bottom - 胶囊height - 状态栏height (现胶囊bottom 为距离导航栏底部的长度)
  71.       bottom: headerPosi.bottom - headerPosi.height - statusBarHeight,
  72.       // 屏幕宽度 - 胶囊right
  73.       right: this.systeminfo.screenWidth - headerPosi.right
  74. }
  75. let haveBack;
  76. if (getCurrentPages().length === 1) { // 当只有一个页面时
  77.      haveBack = false;
  78. } else {
  79.      haveBack = true;
  80. }
  81.     this.haveBack=haveBack, // 获取是否是通过分享进入的小程序
  82.     this.statusBarHeight=statusBarHeight,
  83.     this.navbarHeight= headerPosi.bottom + btnPosi.bottom, // 原胶囊bottom + 现胶囊bottom
  84. // console.log(this.navbarHeight);
  85.     this.navbarBtn=btnPosi
  86. },
  87. onLoad(){
  88. },
  89. methods:{
  90. _goBack: function () {
  91.       uni.navigateBack({
  92.         delta: this.page
  93.       });
  94.     },
  95.     _goHome: function () {
  96.       uni.switchTab({
  97.         url: '/pages/tabBar/home/home'
  98.       })
  99.     }
  100. },
  101. onShow(){
  102. }
  103. }
  104. </script>
  105. <style lang="scss">
  106. .navbar-wrap {
  107. position: fixed;
  108. width: 100%;
  109. top: 0;
  110. z-index: 9999;
  111. box-sizing: border-box;
  112. background: #FFFFFF;
  113. border-bottom: 1px solid #F8F8F8;
  114. }
  115. .navbar-text {
  116. text-align: center;
  117. color: #000000;
  118. font-weight: 500;
  119. }
  120. .navbar-icon {
  121. position: fixed;
  122. display: flex;
  123. border-radius: 64rpx;
  124. border: 0.5px solid rgba(0,0,0, 0.1);
  125. box-sizing: border-box;
  126. }
  127. .navbar-icon .iconfont {
  128. text-align: center;
  129. display: inline-block;
  130. overflow: hidden;
  131. margin-bottom: 1px;
  132. }
  133. .navbar-icon .icon-vertical_line {
  134. color: #ebebeb;
  135. }
  136. .navbar-loading {
  137. background: #fff;
  138. text-align: center;
  139. }
  140. </style>