cm-header.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template name="headerNavbar">
  2. <!-- 自定义导航栏 -->
  3. <view
  4. class="navbar-wrap"
  5. :class="[headerColor ? 'bg-color' : 'bg-color']"
  6. :style="{ height: navbarHeight + 'px', paddingTop: CustomBar / 2 + 'px' }"
  7. >
  8. <view
  9. class="navbar-icon"
  10. v-if="navbarData.showCapsule ? navbarData.showCapsule : true"
  11. :style="{
  12. top: navbarBtn.top + statusBarHeight + 'px;',
  13. lineHeight: navbarBtn.height + 'px;',
  14. left: 12 + 'px;',
  15. width: navbarBtn.height + 'px;',
  16. height: navbarBtn.height + 'px;'
  17. }"
  18. >
  19. <text v-if="haveBack" @click="_goBack" class="iconfont icon-fanhui"></text>
  20. <text v-else @click="_goHome" class="iconfont icon-shouye"></text>
  21. </view>
  22. <view
  23. class="navbar-text"
  24. :style="{
  25. color: navbarData.textColor ? navbarData.textColor : '',
  26. lineHeight: CustomBar - statusBarHeight + 'px;',
  27. fontSize: fontSizeSetting + 'px;',
  28. }"
  29. :class="platformClass"
  30. >
  31. {{ navbarData.title ? navbarData.title : ' ' }}
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. var self
  37. export default {
  38. name: 'headerNavbar',
  39. props: {
  40. navbarData: {
  41. // 由父页面传递的数据
  42. type: Object
  43. },
  44. systeminfo: {
  45. type: Object
  46. },
  47. headerBtnPosi: {
  48. type: Object
  49. },
  50. page: {
  51. type: Number
  52. },
  53. headerColor: {
  54. type: Boolean
  55. },
  56. headerTitle: {
  57. type: String
  58. },
  59. type: {
  60. type: String
  61. }
  62. },
  63. data() {
  64. return {
  65. headerType: '',
  66. CustomBar: this.CustomBar, // 顶部导航栏高度
  67. clickPath: '/pages/search/search',
  68. haveBack: true, // 是否有返回按钮,true 有 false 没有 若从分享页进入则为 false
  69. statusBarHeight: 0, // 状态栏高度
  70. navbarHeight: 0, // 顶部导航栏高度,
  71. navbarBtn: {
  72. // 胶囊位置信息
  73. height: 0,
  74. width: 0,
  75. top: 0,
  76. bottom: 0,
  77. right: 0
  78. },
  79. platform: '',
  80. fontSizeSetting: 0
  81. }
  82. },
  83. created() {
  84. this.headerType = this.type
  85. this.fontSizeSetting = this.systeminfo.fontSizeSetting
  86. let statusBarHeight = this.systeminfo.statusBarHeight // 状态栏高度
  87. let headerPosi = this.headerBtnPosi // 胶囊位置信息
  88. /**
  89. * wx.getMenuButtonBoundingClientRect() 坐标信息以屏幕左上角为原点
  90. * 菜单按键宽度: 87
  91. * 菜单按键高度: 32
  92. * 菜单按键左边界坐标: 278
  93. * 菜单按键上边界坐标: 26
  94. * 菜单按键右边界坐标: 365
  95. * 菜单按键下边界坐标: 58
  96. */
  97. let btnPosi = {
  98. // 胶囊实际位置,坐标信息不是左上角原点
  99. height: headerPosi.height,
  100. width: headerPosi.width,
  101. // 胶囊top - 状态栏高度
  102. top: headerPosi.top - statusBarHeight,
  103. // 胶囊bottom - 胶囊height - 状态栏height (现胶囊bottom 为距离导航栏底部的长度)
  104. bottom: headerPosi.bottom - headerPosi.height - statusBarHeight,
  105. // 屏幕宽度 - 胶囊right
  106. right: this.systeminfo.screenWidth - headerPosi.right
  107. }
  108. console.log(btnPosi)
  109. let haveBack
  110. if (getCurrentPages().length === 1) {
  111. // 当只有一个页面时
  112. haveBack = false
  113. } else {
  114. haveBack = true
  115. }
  116. ;(this.haveBack = haveBack), // 获取是否是通过分享进入的小程序
  117. (this.statusBarHeight = statusBarHeight),
  118. (this.navbarHeight = headerPosi.bottom + btnPosi.bottom), // 原胶囊bottom + 现胶囊bottom
  119. (this.$parent.navbarHeight = this.navbarHeight)
  120. this.$parent.statusBarHeight = this.statusBarHeight
  121. // console.log(this.navbarHeight);
  122. this.navbarBtn = btnPosi
  123. },
  124. onLoad() {},
  125. methods: {
  126. _goBack: function() {
  127. uni.navigateBack({
  128. delta: this.page
  129. })
  130. },
  131. _goHome: function() {
  132. uni.switchTab({
  133. url: '/pages/tabBar/home/index'
  134. })
  135. }
  136. },
  137. onShow() {}
  138. }
  139. </script>
  140. <style lang="scss">
  141. .navbar-wrap {
  142. position: fixed;
  143. width: 100%;
  144. top: 0;
  145. z-index: 100000;
  146. box-sizing: border-box;
  147. &.bg-color {
  148. animation: showColor 0.3s ease-in-out both;
  149. }
  150. &.no-color {
  151. animation: hideColor 0.3s ease-in-out both;
  152. }
  153. }
  154. .navbar-text {
  155. width: 100%;
  156. color: #000000;
  157. font-weight: 500;
  158. position: fixed;
  159. text-align: center;
  160. }
  161. .navbar-icon {
  162. position: fixed;
  163. display: flex;
  164. border-radius: 50%;
  165. text-align: center;
  166. background: rgba(255, 255, 255, 0.6);
  167. border: 0.5px solid rgba(0, 0, 0, 0.1);
  168. box-sizing: border-box;
  169. z-index: 9999;
  170. }
  171. .navbar-icon .iconfont {
  172. height: 100%;
  173. width: 100%;
  174. font-size: 38rpx;
  175. font-weight: bold;
  176. display: inline-block;
  177. overflow: hidden;
  178. }
  179. .navbar-icon view {
  180. height: 18px;
  181. border-left: 0.5px solid rgba(0, 0, 0, 0.3);
  182. margin-top: 6px;
  183. }
  184. .navbar-loading {
  185. background: #fff;
  186. text-align: center;
  187. }
  188. @keyframes showColor {
  189. 0% {
  190. background: rgba(255, 255, 255, 0);
  191. }
  192. 50% {
  193. background: rgba(255, 255, 255, 0.5);
  194. }
  195. 100% {
  196. background: rgba(255, 255, 255, 1);
  197. }
  198. }
  199. @keyframes hideColor {
  200. 0% {
  201. background: rgba(255, 255, 255, 1);
  202. }
  203. 50% {
  204. background: rgba(255, 255, 255, 0.5);
  205. }
  206. 100% {
  207. background: rgba(255, 255, 255, 0);
  208. }
  209. }
  210. </style>