tui-navigation-bar.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="tui-navigation-bar"
  3. :class="{ 'tui-bar-line': opacity > 0.85 && splitLine, 'tui-navbar-fixed': isFixed, 'tui-backdrop__filter': backdropFilter && dropDownOpacity > 0 }"
  4. :style="{ height: height + 'px', backgroundColor: `rgba(${background},${opacity})`, opacity: dropDownOpacity, zIndex: isFixed ? zIndex : 'auto' }">
  5. <view class="tui-status-bar" :style="{ height: statusBarHeight + 'px' }" v-if="isImmersive"></view>
  6. <view class="tui-navigation_bar-title"
  7. :style="{ opacity: transparent || opacity >= maxOpacity ? 1 : opacity, color: color, paddingTop: top - statusBarHeight + 'px' }"
  8. v-if="title && !isCustom">
  9. {{ title }}
  10. </view>
  11. <slot></slot>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'tuiNavigationBar',
  17. emits: ['init', 'change'],
  18. props: {
  19. //NavigationBar标题
  20. title: {
  21. type: String,
  22. default: ''
  23. },
  24. //NavigationBar标题颜色
  25. color: {
  26. type: String,
  27. default: '#333'
  28. },
  29. //NavigationBar背景颜色,不支持rgb
  30. backgroundColor: {
  31. type: String,
  32. default: '#fff'
  33. },
  34. //是否需要分割线
  35. splitLine: {
  36. type: Boolean,
  37. default: false
  38. },
  39. //是否设置不透明度
  40. isOpacity: {
  41. type: Boolean,
  42. default: true
  43. },
  44. //不透明度最大值 0-1
  45. maxOpacity: {
  46. type: [Number, String],
  47. default: 1
  48. },
  49. //背景透明 【设置该属性,则背景透明,只出现内容,isOpacity和maxOpacity失效】
  50. transparent: {
  51. type: Boolean,
  52. default: false
  53. },
  54. //滚动条滚动距离
  55. scrollTop: {
  56. type: [Number, String],
  57. default: 0
  58. },
  59. /*
  60. isOpacity 为true时生效
  61. opacity=scrollTop /windowWidth * scrollRatio
  62. */
  63. scrollRatio: {
  64. type: [Number, String],
  65. default: 0.3
  66. },
  67. //是否自定义header内容
  68. isCustom: {
  69. type: Boolean,
  70. default: false
  71. },
  72. //是否沉浸式
  73. isImmersive: {
  74. type: Boolean,
  75. default: true
  76. },
  77. isFixed: {
  78. type: Boolean,
  79. default: true
  80. },
  81. //是否开启高斯模糊效果[仅在支持的浏览器有效果]
  82. backdropFilter: {
  83. type: Boolean,
  84. default: false
  85. },
  86. //下拉隐藏NavigationBar,主要针对有回弹效果ios端
  87. dropDownHide: {
  88. type: Boolean,
  89. default: false
  90. },
  91. //z-index设置
  92. zIndex: {
  93. type: [Number, String],
  94. default: 996
  95. }
  96. },
  97. watch: {
  98. scrollTop(newValue, oldValue) {
  99. if (this.isOpacity && !this.transparent) {
  100. this.opacityChange();
  101. }
  102. },
  103. backgroundColor(val) {
  104. if (val) {
  105. this.background = this.hexToRgb(val);
  106. }
  107. }
  108. },
  109. data() {
  110. return {
  111. width: 375, //header宽度
  112. left: 375, //小程序端 左侧距胶囊按钮距离
  113. height: 44, //header高度
  114. top: 0,
  115. scrollH: 1, //滚动总高度,计算opacity
  116. opacity: 1, //0-1
  117. statusBarHeight: 0, //状态栏高度
  118. background: '255,255,255', //header背景色
  119. dropDownOpacity: 1
  120. };
  121. },
  122. created() {
  123. this.dropDownOpacity = this.backdropFilter && 0;
  124. this.opacity = this.isOpacity || this.transparent ? 0 : this.maxOpacity;
  125. this.background = this.hexToRgb(this.backgroundColor);
  126. let obj = {};
  127. // #ifdef MP-WEIXIN
  128. obj = wx.getMenuButtonBoundingClientRect();
  129. // #endif
  130. // #ifdef MP-BAIDU
  131. obj = swan.getMenuButtonBoundingClientRect();
  132. // #endif
  133. // #ifdef MP-ALIPAY
  134. my.hideAddToDesktopMenu();
  135. // #endif
  136. uni.getSystemInfo({
  137. success: res => {
  138. this.statusBarHeight = res.statusBarHeight;
  139. this.width = res.windowWidth;
  140. this.left = obj.left || res.windowWidth;
  141. if (this.isImmersive) {
  142. this.height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
  143. }
  144. this.scrollH = res.windowWidth * this.scrollRatio;
  145. this.top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
  146. this.$emit('init', {
  147. width: this.width,
  148. height: this.height,
  149. left: this.left,
  150. top: this.top,
  151. statusBarHeight: this.statusBarHeight,
  152. opacity: this.opacity,
  153. windowHeight: res.windowHeight
  154. });
  155. }
  156. });
  157. },
  158. methods: {
  159. hexToRgb(hex) {
  160. let rgb = '255,255,255';
  161. if (hex && ~hex.indexOf('#')) {
  162. if (hex.length === 4) {
  163. let text = hex.substring(1, 4);
  164. hex = '#' + text + text;
  165. }
  166. let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  167. if (result) {
  168. rgb = `${parseInt(result[1], 16)},${parseInt(result[2], 16)},${parseInt(result[3], 16)}`;
  169. }
  170. }
  171. return rgb;
  172. },
  173. opacityChange() {
  174. if (this.dropDownHide) {
  175. if (this.scrollTop < 0) {
  176. if (this.dropDownOpacity > 0) {
  177. this.dropDownOpacity = 1 - Math.abs(this.scrollTop) / 30;
  178. }
  179. } else {
  180. this.dropDownOpacity = 1;
  181. }
  182. }
  183. let scroll = this.scrollTop <= 1 ? 0 : this.scrollTop;
  184. let opacity = scroll / this.scrollH;
  185. if ((this.opacity >= this.maxOpacity && opacity >= this.maxOpacity) || (this.opacity == 0 && opacity ==
  186. 0)) {
  187. return;
  188. }
  189. this.opacity = opacity > this.maxOpacity ? this.maxOpacity : opacity;
  190. if (this.backdropFilter) {
  191. this.dropDownOpacity = this.opacity >= this.maxOpacity ? 1 : this.opacity;
  192. }
  193. this.$emit('change', {
  194. opacity: this.opacity
  195. });
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. .tui-navigation-bar {
  202. width: 100%;
  203. transition: opacity 0.4s;
  204. }
  205. .tui-backdrop__filter {
  206. /* Safari for macOS & iOS */
  207. -webkit-backdrop-filter: blur(15px);
  208. /* Google Chrome */
  209. backdrop-filter: blur(15px);
  210. }
  211. .tui-navbar-fixed {
  212. position: fixed;
  213. left: 0;
  214. top: 0;
  215. }
  216. .tui-status-bar {
  217. width: 100%;
  218. }
  219. .tui-navigation_bar-title {
  220. width: 100%;
  221. font-size: 17px;
  222. line-height: 17px;
  223. /* #ifndef APP-PLUS */
  224. font-weight: 500;
  225. /* #endif */
  226. height: 32px;
  227. display: flex;
  228. align-items: center;
  229. justify-content: center;
  230. }
  231. .tui-bar-line::after {
  232. content: '';
  233. position: absolute;
  234. border-bottom: 1rpx solid #eaeef1;
  235. -webkit-transform: scaleY(0.5);
  236. transform: scaleY(0.5);
  237. bottom: 0;
  238. right: 0;
  239. left: 0;
  240. }
  241. </style>