header-poduct.vue 4.8 KB

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