tui-navigation-bar.vue 5.7 KB

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