tui-navigation-bar.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view
  3. class="tui-navigation-bar"
  4. :class="{ 'tui-bar-line': opcity > 0.85 && splitLine, 'tui-navbar-fixed': isFixed }"
  5. :style="{ height: height + 'px', backgroundColor: `rgba(${backgroundColor},${opcity})` }"
  6. >
  7. <view class="tui-status-bar" :style="{ height: statusBarHeight + 'px' }" v-if="isImmersive"></view>
  8. <view class="tui-navigation_bar-title" :style="{ opacity: opcity, color: color, paddingTop: top - statusBarHeight + 'px' }" v-if="title && !isCustom">{{ title }}</view>
  9. <slot />
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'tuiNavigationBar',
  15. props: {
  16. //NavigationBar标题
  17. title: {
  18. type: String,
  19. default: ''
  20. },
  21. //NavigationBar标题颜色
  22. color: {
  23. type: String,
  24. default: '#fff'
  25. },
  26. //NavigationBar背景颜色 rgb
  27. backgroundColor: {
  28. type: String,
  29. default: '86,119,252'
  30. },
  31. //是否需要分割线
  32. splitLine: {
  33. type: Boolean,
  34. default: false
  35. },
  36. //是否设置不透明度
  37. isOpcity: {
  38. type: Boolean,
  39. default: true
  40. },
  41. //滚动条滚动距离
  42. scrollTop: {
  43. type: [Number, String],
  44. default: 0
  45. },
  46. /*
  47. isOpcity 为true时生效
  48. opcity=scrollTop /windowWidth * scrollRatio
  49. */
  50. scrollRatio: {
  51. type: [Number, String],
  52. default: 0.3
  53. },
  54. //是否自定义header内容
  55. isCustom: {
  56. type: Boolean,
  57. default: false
  58. },
  59. //是否沉浸式
  60. isImmersive: {
  61. type: Boolean,
  62. default: true
  63. },
  64. isFixed: {
  65. type: Boolean,
  66. default: true
  67. }
  68. },
  69. watch: {
  70. scrollTop(newValue, oldValue) {
  71. if (this.isOpcity) {
  72. this.opcityChange();
  73. }
  74. }
  75. },
  76. data() {
  77. return {
  78. width: 375, //header宽度
  79. left: 375, //小程序端 左侧距胶囊按钮距离
  80. height: 44, //header高度
  81. top: 0,
  82. scrollH: 1, //滚动总高度,计算opcity
  83. opcity: 0, //0-1
  84. statusBarHeight: 0 //状态栏高度
  85. };
  86. },
  87. created() {
  88. this.opcity = this.isOpcity ? this.opcity : 1;
  89. let obj = {};
  90. // #ifdef MP-WEIXIN
  91. obj = wx.getMenuButtonBoundingClientRect();
  92. // #endif
  93. // #ifdef MP-BAIDU
  94. obj = swan.getMenuButtonBoundingClientRect();
  95. // #endif
  96. // #ifdef MP-ALIPAY
  97. my.hideAddToDesktopMenu();
  98. // #endif
  99. uni.getSystemInfo({
  100. success: res => {
  101. this.statusBarHeight = res.statusBarHeight;
  102. this.width = res.windowWidth;
  103. this.left = obj.left || res.windowWidth;
  104. if (this.isImmersive) {
  105. this.height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
  106. }
  107. this.scrollH = res.windowWidth * this.scrollRatio;
  108. this.top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
  109. this.$emit('init', {
  110. width: this.width,
  111. height: this.height,
  112. left: obj.left,
  113. top: this.top,
  114. statusBarHeight: this.statusBarHeight,
  115. opcity: this.opcity
  116. });
  117. }
  118. });
  119. },
  120. methods: {
  121. opcityChange() {
  122. let scroll = this.scrollTop <= 1 ? 0 : this.scrollTop;
  123. let opcity = scroll / this.scrollH;
  124. if ((this.opcity >= 1 && opcity >= 1) || (this.opcity == 0 && opcity == 0)) {
  125. return;
  126. }
  127. this.opcity = opcity;
  128. this.$emit('change', {
  129. opcity: this.opcity
  130. });
  131. }
  132. }
  133. };
  134. </script>
  135. <style scoped>
  136. .tui-navigation-bar {
  137. width: 100%;
  138. }
  139. .tui-navbar-fixed {
  140. position: fixed;
  141. left: 0;
  142. top: 0;
  143. z-index: 9998;
  144. }
  145. .tui-status-bar {
  146. width: 100%;
  147. }
  148. .tui-navigation_bar-title {
  149. width: 100%;
  150. font-size: 17px;
  151. line-height: 17px;
  152. /* #ifndef APP-PLUS */
  153. font-weight: 500;
  154. /* #endif */
  155. height: 32px;
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. }
  160. .tui-bar-line::after {
  161. content: '';
  162. position: absolute;
  163. border-bottom: 1rpx solid #eaeef1;
  164. -webkit-transform: scaleY(0.5);
  165. transform: scaleY(0.5);
  166. bottom: 0;
  167. right: 0;
  168. left: 0;
  169. }
  170. </style>