header-back.vue 5.1 KB

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