u-navbar.vue 11 KB

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