header-cart.vue 5.0 KB

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