u-navbar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <template>
  2. <view class="">
  3. <view class="u-navbar" :style="[navbarStyle]"
  4. :class="{ 'u-navbar-fixed': isFixed, 'u-border-bottom': borderBottom }">
  5. <view class="u-status-bar" :style="{ height: statusBarHeight* + 'rpx' }"></view>
  6. <view class="u-navbar-inner" :style="[navbarInnerStyle]">
  7. <view class="u-back-wrap" v-if="isBack" @tap="goBack">
  8. <view class="u-icon-wrap">
  9. <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
  10. </view>
  11. <view class="u-icon-wrap u-back-text u-line-1" v-if="backText" :style="[backTextStyle]">
  12. {{ backText }}</view>
  13. </view>
  14. <view class="u-navbar-content-title" v-if="title">
  15. <view class="u-title u-line-1" :style="{
  16. color: titleColor,
  17. fontSize: titleSize + 'rpx',
  18. fontWeight: titleBold ? 'bold' : 'normal'
  19. }">
  20. {{ title }}
  21. </view>
  22. </view>
  23. <view class="u-slot-content">
  24. <slot></slot>
  25. </view>
  26. <view class="u-slot-right">
  27. <slot name="right"></slot>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 解决fixed定位后导航栏塌陷的问题 -->
  32. <view class="u-navbar-placeholder" v-if="isFixed && !immersive"
  33. :style="{ width: '100%', height: (Number(navbarHeight) + statusBarHeight)*2 + 'rpx' }"></view>
  34. </view>
  35. </template>
  36. <script>
  37. import {
  38. getUrlParam,
  39. } from '@/apis/utils'
  40. // 获取系统状态栏的高度
  41. let systemInfo = uni.getSystemInfoSync();
  42. let menuButtonInfo = {};
  43. // 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API,尚未兼容)
  44. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  45. menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  46. // #endif
  47. /**
  48. * navbar 自定义导航栏
  49. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uniapp自带的导航栏。
  50. * @tutorial https://www.uviewui.com/components/navbar.html
  51. * @property {String Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上),注意这里的单位是px(默认44)
  52. * @property {String} back-icon-color 左边返回图标的颜色(默认#606266)
  53. * @property {String} back-icon-name 左边返回图标的名称,只能为uView自带的图标(默认arrow-left)
  54. * @property {String Number} back-icon-size 左边返回图标的大小,单位rpx(默认30)
  55. * @property {String} back-text 返回图标右边的辅助提示文字
  56. * @property {Object} back-text-style 返回图标右边的辅助提示文字的样式,对象形式(默认{ color: '#606266' })
  57. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  58. * @property {String Number} title-width 导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpx(默认250)
  59. * @property {String} title-color 标题的颜色(默认#606266)
  60. * @property {String Number} title-size 导航栏标题字体大小,单位rpx(默认32)
  61. * @property {Function} custom-back 自定义返回逻辑方法
  62. * @property {String Number} z-index 固定在顶部时的z-index值(默认980)
  63. * @property {Boolean} is-back 是否显示导航栏左边返回图标和辅助文字(默认true)
  64. * @property {Object} background 导航栏背景设置,见官网说明(默认{ background: '#ffffff' })
  65. * @property {Boolean} is-fixed 导航栏是否固定在顶部(默认true)
  66. * @property {Boolean} immersive 沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效(默认false)
  67. * @property {Boolean} border-bottom 导航栏底部是否显示下边框,如定义了较深的背景颜色,可取消此值(默认true)
  68. * @example <u-navbar back-text="返回" title="剑未配妥,出门已是江湖"></u-navbar>
  69. */
  70. export default {
  71. name: "u-navbar",
  72. props: {
  73. // 导航栏高度,单位px,非rpx
  74. height: {
  75. type: [String, Number],
  76. default: ''
  77. },
  78. // 返回箭头的颜色
  79. backIconColor: {
  80. type: String,
  81. default: '#101010'
  82. },
  83. // 左边返回的图标
  84. backIconName: {
  85. type: String,
  86. default: 'nav-back'
  87. },
  88. // 左边返回图标的大小,rpx
  89. backIconSize: {
  90. type: [String, Number],
  91. default: '44'
  92. },
  93. // 返回的文字提示
  94. backText: {
  95. type: String,
  96. default: ''
  97. },
  98. // 返回的文字的 样式
  99. backTextStyle: {
  100. type: Object,
  101. default () {
  102. return {
  103. color: '#606266'
  104. }
  105. }
  106. },
  107. // 导航栏标题
  108. title: {
  109. type: String,
  110. default: ''
  111. },
  112. titleLong: {
  113. type: String,
  114. default: ''
  115. },
  116. // 标题的宽度,如果需要自定义右侧内容,且右侧内容很多时,可能需要减少这个宽度,单位rpx
  117. titleWidth: {
  118. type: [String, Number],
  119. default: '250'
  120. },
  121. // 标题的颜色
  122. titleColor: {
  123. type: String,
  124. default: '#101010'
  125. },
  126. // 标题字体是否加粗
  127. titleBold: {
  128. type: Boolean,
  129. default: false
  130. },
  131. // 标题的字体大小
  132. titleSize: {
  133. type: [String, Number],
  134. default: 32
  135. },
  136. isBack: {
  137. type: [Boolean, String],
  138. default: true
  139. },
  140. // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
  141. background: {
  142. type: Object,
  143. default () {
  144. return {
  145. background: '#ffffff'
  146. }
  147. }
  148. },
  149. // 导航栏是否固定在顶部
  150. isFixed: {
  151. type: Boolean,
  152. default: true
  153. },
  154. // 是否沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效
  155. immersive: {
  156. type: Boolean,
  157. default: false
  158. },
  159. // 是否显示导航栏的下边框
  160. borderBottom: {
  161. type: Boolean,
  162. default: true
  163. },
  164. zIndex: {
  165. type: [String, Number],
  166. default: ''
  167. },
  168. bkUrl:{
  169. type: [String, Number],
  170. default: ''
  171. },
  172. // 自定义返回逻辑
  173. customBack: {
  174. type: Function,
  175. default: null
  176. }
  177. },
  178. data() {
  179. return {
  180. menuButtonInfo: menuButtonInfo,
  181. statusBarHeight: systemInfo.statusBarHeight
  182. };
  183. },
  184. computed: {
  185. // 导航栏内部盒子的样式
  186. navbarInnerStyle() {
  187. let style = {};
  188. // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
  189. style.height = this.navbarHeight * 2 + 'rpx';
  190. // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
  191. // #ifdef MP
  192. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  193. style.marginRight = rightButtonWidth + 'px';
  194. // #endif
  195. return style;
  196. },
  197. // 整个导航栏的样式
  198. navbarStyle() {
  199. let style = {};
  200. style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.navbar;
  201. // 合并用户传递的背景色对象
  202. Object.assign(style, this.background);
  203. return style;
  204. },
  205. // 导航中间的标题的样式
  206. titleStyle() {
  207. let style = {};
  208. // #ifndef MP
  209. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  210. style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  211. // #endif
  212. // #ifdef MP
  213. // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
  214. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  215. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  216. style.right = rightButtonWidth - (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 +
  217. rightButtonWidth +
  218. 'px';
  219. // #endif
  220. //style.width = uni.upx2px(this.titleWidth) + 'px';
  221. return style;
  222. },
  223. // 转换字符数值为真正的数值
  224. navbarHeight() {
  225. // #ifdef APP-PLUS || H5
  226. return this.height ? this.height : 44;
  227. // #endif
  228. // #ifdef MP
  229. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  230. // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
  231. // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  232. let height = systemInfo.platform == 'ios' ? 44 : 48;
  233. return this.height ? this.height : height;
  234. // #endif
  235. }
  236. },
  237. created() {},
  238. methods: {
  239. navberBack() {
  240. const pages = getCurrentPages()
  241. if (pages.length === 1) {
  242. history.back()
  243. } else {
  244. uni.navigateBack();
  245. }
  246. },
  247. goBack() {
  248. //console.log("修复bug, 刷新后不能返回问题")
  249. if(this.bkUrl){
  250. uni.switchTab({
  251. url:this.bkUrl
  252. })
  253. }
  254. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  255. else if (typeof this.customBack === 'function') {
  256. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  257. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  258. this.customBack.bind(this.$u.$parent.call(this))();
  259. } else {
  260. this.navberBack();
  261. }
  262. }
  263. }
  264. };
  265. </script>
  266. <style scoped lang="scss">
  267. @import "../../libs/css/style.components.scss";
  268. .u-navbar {
  269. width: 100%;
  270. }
  271. .u-navbar-fixed {
  272. position: fixed;
  273. left: 0;
  274. right: 0;
  275. top: 0;
  276. z-index: 991;
  277. }
  278. .u-status-bar {
  279. width: 100%;
  280. }
  281. .u-navbar-inner {
  282. @include vue-flex;
  283. justify-content: space-between;
  284. position: relative;
  285. align-items: center;
  286. }
  287. .u-back-wrap {
  288. @include vue-flex;
  289. align-items: center;
  290. flex: 1;
  291. flex-grow: 0;
  292. z-index: 999;
  293. padding: 14rpx 14rpx 14rpx 24rpx;
  294. }
  295. .u-back-text {
  296. padding-left: 4rpx;
  297. font-size: 30rpx;
  298. }
  299. .u-navbar-content-title {
  300. @include vue-flex;
  301. align-items: center;
  302. justify-content: center;
  303. flex: 1;
  304. position: absolute;
  305. left: 0;
  306. right: 0;
  307. height: 60rpx;
  308. text-align: center;
  309. flex-shrink: 0;
  310. }
  311. .u-navbar-centent-slot {
  312. flex: 1;
  313. }
  314. .u-title {
  315. line-height: 60rpx;
  316. font-size: 32rpx;
  317. flex: 1;
  318. }
  319. .u-slot-right {
  320. z-index: 99;
  321. }
  322. .u-navbar-right {
  323. flex: 1;
  324. @include vue-flex;
  325. align-items: center;
  326. justify-content: flex-end;
  327. }
  328. .u-slot-content {
  329. flex: 1;
  330. @include vue-flex;
  331. align-items: center;
  332. }
  333. </style>