u-navbar.vue 10 KB

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