header-back.vue 4.6 KB

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