u-navbar.vue 9.9 KB

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