u-navbar.vue 11 KB

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