u-navbar.vue 9.8 KB

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