u-navbar.vue 10 KB

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