You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

296 lines
8.6 KiB

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