header-back.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. isUsertype:{
  38. type:Number
  39. }
  40. },
  41. data() {
  42. return{
  43. haveBack: true, // 是否有返回按钮,true 有 false 没有 若从分享页进入则为 false
  44.     statusBarHeight: 0, // 状态栏高度
  45.     navbarHeight: 0, // 顶部导航栏高度
  46.     navbarBtn: { // 胶囊位置信息
  47.       height: 0,
  48.       width: 0,
  49.       top: 0,
  50.       bottom: 0,
  51.       right: 0
  52. },
  53. platformClass:'',
  54. fontSizeSetting:0,
  55. }
  56. },
  57. created() {
  58. let statusBarHeight = this.systeminfo.statusBarHeight // 状态栏高度
  59. let headerPosi = this.headerBtnPosi // 胶囊位置信息
  60. this.fontSizeSetting = 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.     this.navbarBtn=btnPosi
  95. },
  96. onLoad(){
  97. },
  98. methods:{
  99. _goBack: function () {
  100. if(this.isShare){
  101. this._goHome()
  102. }else if(this.isDelete){
  103. this._goUser()
  104. }else{
  105. uni.navigateBack({
  106.         delta: 1
  107.       });
  108. }
  109.     },
  110.     _goHome: function () {
  111.       uni.switchTab({
  112.         url: '/pages/tabBar/home/home'
  113.       })
  114.     },
  115.     _goUser: function () {
  116.       uni.switchTab({
  117.         url: '/pages/tabBar/user/user'
  118.       })
  119.     },
  120. _goSearchPath:function () {
  121. this.$emit('goSearchPath')
  122. }
  123. },
  124. onShow(){
  125. }
  126. }
  127. </script>
  128. <style lang="scss">
  129. .navbar-wrap {
  130. position: fixed;
  131. width: 100%;
  132. top: 0;
  133. z-index: 100000;
  134. box-sizing: border-box;
  135. background: #FFFFFF;
  136. }
  137. .navbar-text {
  138. font-size: 30rpx;
  139. color: #000000;
  140. font-weight: 500;
  141. }
  142. .navbar-text.center{
  143. text-align: center;
  144. }
  145. .navbar-text.left{
  146. text-align: left;
  147. padding-left: 38px;
  148. }
  149. .navbar-icon {
  150. position: fixed;
  151. display: flex;
  152. box-sizing: border-box;
  153. }
  154. .navbar-icon .iconfont {
  155. display: inline-block;
  156. overflow: hidden;
  157. font-size: 42rpx;
  158. padding-right:40rpx;
  159. margin-top: 1px;
  160. }
  161. .navbar-icon .icon-iconfonticonfontsousuo1 {
  162. color: #000000;
  163. }
  164. .navbar-icon view {
  165. height: 18px;
  166. border-left: 0.5px solid rgba(0,0,0, 0.3);
  167. margin-top: 6px;
  168. }
  169. .navbar-loading {
  170. background: #fff;
  171. text-align: center;
  172. }
  173. </style>