header-poduct.vue 4.9 KB

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