ws-custom.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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='navbarData.haveBack' @click="_goBack" class="iconfont icon-daohangfanhui" :style="{width:headerBtnPosi.width/2+'px',height:navbarBtn.height+'px'}"></text>
  10.       <text v-if='navbarData.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-fanhuishouye" :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. isBackType:{
  30. type:Boolean,
  31. default:false
  32. },
  33. page:{
  34. type:Number
  35. },
  36. path:{
  37. type:String
  38. }
  39. },
  40. data() {
  41. return{
  42. // haveBack: true, // 是否有返回按钮,true 有 false 没有 若从分享页进入则为 false
  43.     statusBarHeight: 0, // 状态栏高度
  44.     navbarHeight: 0, // 顶部导航栏高度
  45.     navbarBtn: { // 胶囊位置信息
  46.       height: 0,
  47.       width: 0,
  48.       top: 0,
  49.       bottom: 0,
  50.       right: 0
  51. },
  52. platform:'',
  53. fontSizeSetting:0,
  54. screenWidth:0
  55. }
  56. },
  57. created() {
  58. this.fontSizeSetting = this.systeminfo.fontSizeSetting
  59. let statusBarHeight = this.systeminfo.statusBarHeight // 状态栏高度
  60. let headerPosi = this.headerBtnPosi // 胶囊位置信息
  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.     this.navbarBtn=btnPosi
  90. },
  91. onLoad(){
  92. },
  93. methods:{
  94. _goBack: function () {
  95. let self = this;
  96. if(this.isBackType){
  97. uni.navigateTo({
  98.         url: self.path
  99.       })
  100. }else{
  101. uni.navigateBack({
  102.         delta: this.page
  103.       });
  104. }
  105.      
  106.     },
  107.     _goHome: function () {
  108.       uni.navigateTo({
  109.         url: '/pages/index/index'
  110.       })
  111.     }
  112. },
  113. onShow(){
  114. }
  115. }
  116. </script>
  117. <style lang="scss">
  118. .navbar-wrap {
  119. position: fixed;
  120. width: 100%;
  121. top: 0;
  122. z-index: 9999;
  123. box-sizing: border-box;
  124. background: #FFFFFF;
  125. }
  126. .navbar-text {
  127. text-align: center;
  128. color: #000000;
  129. font-weight: 500;
  130. }
  131. .navbar-icon {
  132. position: fixed;
  133. display: flex;
  134. border-radius: 64rpx;
  135. border: 0.5px solid rgba(0,0,0, 0.2);
  136. box-sizing: border-box;
  137. }
  138. .navbar-icon .iconfont {
  139. text-align: center;
  140. display: inline-block;
  141. overflow: hidden;
  142. margin-bottom: 1px;
  143. &.icon-fanhuishouye {
  144. font-size: 38rpx;
  145. }
  146. }
  147. .navbar-icon .icon-vertical_line {
  148. color: #999999;
  149. }
  150. .navbar-loading {
  151. background: #fff;
  152. text-align: center;
  153. }
  154. </style>