tui-navigation-bar.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view
  3. class="tui-navigation-bar"
  4. :class="{ 'tui-bar-line': opacity > 0.85 && splitLine, 'tui-navbar-fixed': isFixed, 'tui-backdrop__filter': backdropFilter }"
  5. :style="{ height: height + 'px', backgroundColor:backgroundColor,opacity:opacity }"
  6. >
  7. <view class="tui-status-bar" :style="{ height: statusBarHeight + 'px' }" v-if="isImmersive"></view>
  8. <view class="tui-navigation_bar-title" :style="{ opacity: opacity, 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: '#333'
  25. },
  26. //NavigationBar背景颜色
  27. backgroundColor: {
  28. type: String,
  29. default: '#fff'
  30. },
  31. //是否需要分割线
  32. splitLine: {
  33. type: Boolean,
  34. default: false
  35. },
  36. //是否设置不透明度
  37. isOpacity: {
  38. type: Boolean,
  39. default: true
  40. },
  41. //滚动条滚动距离
  42. scrollTop: {
  43. type: [Number, String],
  44. default: 0
  45. },
  46. /*
  47. isOpacity 为true时生效
  48. opacity=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. backdropFilter: {
  70. type: Boolean,
  71. default: false
  72. }
  73. },
  74. watch: {
  75. scrollTop(newValue, oldValue) {
  76. if (this.isOpacity) {
  77. this.opacityChange();
  78. }
  79. }
  80. },
  81. data() {
  82. return {
  83. width: 375, //header宽度
  84. left: 375, //小程序端 左侧距胶囊按钮距离
  85. height: 44, //header高度
  86. top: 0,
  87. scrollH: 1, //滚动总高度,计算opacity
  88. opacity: 1, //0-1
  89. statusBarHeight: 0 //状态栏高度
  90. };
  91. },
  92. created() {
  93. this.opacity = this.isOpacity ? 0 : 1;
  94. let obj = {};
  95. // #ifdef MP-WEIXIN
  96. obj = wx.getMenuButtonBoundingClientRect();
  97. // #endif
  98. // #ifdef MP-BAIDU
  99. obj = swan.getMenuButtonBoundingClientRect();
  100. // #endif
  101. // #ifdef MP-ALIPAY
  102. my.hideAddToDesktopMenu();
  103. // #endif
  104. uni.getSystemInfo({
  105. success: res => {
  106. this.statusBarHeight = res.statusBarHeight;
  107. this.width = res.windowWidth;
  108. this.left = obj.left || res.windowWidth;
  109. if (this.isImmersive) {
  110. this.height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
  111. }
  112. this.scrollH = res.windowWidth * this.scrollRatio;
  113. this.top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
  114. this.$emit('init', {
  115. width: this.width,
  116. height: this.height,
  117. left: obj.left,
  118. top: this.top,
  119. statusBarHeight: this.statusBarHeight,
  120. opacity: this.opacity
  121. });
  122. }
  123. });
  124. },
  125. methods: {
  126. opacityChange() {
  127. let scroll = this.scrollTop <= 1 ? 0 : this.scrollTop;
  128. let opacity = scroll / this.scrollH;
  129. if ((this.opacity >= 1 && opacity >= 1) || (this.opacity == 0 && opacity == 0)) {
  130. return;
  131. }
  132. this.opacity = opacity;
  133. this.$emit('change', {
  134. opacity: this.opacity
  135. });
  136. }
  137. }
  138. };
  139. </script>
  140. <style scoped>
  141. .tui-navigation-bar {
  142. width: 100%;
  143. }
  144. .tui-backdrop__filter {
  145. /* Safari for macOS & iOS */
  146. -webkit-backdrop-filter: blur(15px);
  147. /* Google Chrome */
  148. backdrop-filter: blur(15px);
  149. }
  150. .tui-navbar-fixed {
  151. position: fixed;
  152. left: 0;
  153. top: 0;
  154. z-index: 9998;
  155. }
  156. .tui-status-bar {
  157. width: 100%;
  158. }
  159. .tui-navigation_bar-title {
  160. width: 100%;
  161. font-size: 17px;
  162. line-height: 17px;
  163. /* #ifndef APP-PLUS */
  164. font-weight: 500;
  165. /* #endif */
  166. height: 32px;
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. }
  171. .tui-bar-line::after {
  172. content: '';
  173. position: absolute;
  174. border-bottom: 1rpx solid #eaeef1;
  175. -webkit-transform: scaleY(0.5);
  176. transform: scaleY(0.5);
  177. bottom: 0;
  178. right: 0;
  179. left: 0;
  180. }
  181. </style>